First off, I know there are a number of questions related to this one already asked here (This and This seem most similar). I've read through a number of them, but am finding myself still very confused about the appropriate way to accomplish this.
I have a project that I'm working on that is structured in this way:
--LabAnalysisModules
|-- __init__.py
|-- EphysTools
|-- __init__.py
|-- synaptics.py
|-- utilities.py
|-- PrairieAnalysis
|-- pv_import.py
|-- pxml_parse.py
--PVAnalysis
|-- __init__.py
|-- MainWindow.py
|-- DataViewer.py
|-- AnalysisWindows
|-- __init__.py
|-- AnalysisWidget.py
AnalysisWidget inherits from the DataViewer class in DataViewer. In trying to import DataViewer I have run into two points of confusion.
What is the most appropriate way to actually import DataViewer. I am currently resorting to including sys.path.append(os.path.abspath("../../PVAnalysis"))
in AnalysisWidget, since a relative import gives me SystemError: Parent module '' not loaded, cannot perform relative import
Within DataViewer I have this try...except block (on some of my machines LabAnalysisModules has been permanently added to the python path, and on others it hasn't)
try:
import PrairieAnalysis.pv_import as pvi
import EphysTools.utilities as util
except ImportError:
import os
sys.path.append(os.path.abspath('../LabAnalysisModules'))
import PrairieAnalysis.pv_import as pvi
import EphysTools.utilities as util
Running DataViewer itself works fine, but I run into another import error import DataViewer
from AnalysisWidget
:
ImportError: No module named 'PrairieAnalysis'
Adding print(sys.path)
to the above except
block, this is what is added to sys.path when DataViewer is imported:
E:\\Users\\Dan\\SkyDrive\\Documents\\Python\\PVAnalysis\\LabAnalysisModules'
Which is obviously not correct.
I can fix this import error by including sys.path.append('../../LabAnalysisModules')
in AnalysisWidget
, but this seems like a very kludgy fix
Edit 1
As I say in the comments, ultimately AnalysisWidget will be imported by MainWindow. Again, really not clear on how relative imports work. Getting:
from .. import DataViewer
ValueError: attempted relative import beyond top-level package
At the end of the day, the two things I'm trying to do are:
Be able to simply run AnalysisWidget.py (I know that in general you aren't supposed to run scripts from within a module, but when it comes to creating a GUI I find myself doing this all the time)
Be able to import AnalysisWidget into MainWindow.
If I clean up how I'm doing imports for the LabAnalysisModules as well that would be good.
In order for package imports to work properly in Python 3, your working directory must be the parent of LabAnalysisModules
and PVAnalysis
(or said directory must appear on sys.path
, but making it the working directory is the easiest way to do that). Furthermore, if any of your modules are runnable, they must be invoked with syntax like this:
python -m 'PVAnalysis.AnalysisWindows.AnalysisWidget'
Please consult PEP 328 for more information.