Bug #67
openPython imports, absolute vs. relative
30%
Description
We should be using absolute imports instead of relative imports when possible. This means for instance:
import leginon.leginondata
rather than
import leginondata
There are a few reasons why explained in PEP 328
Python 3 will require either absolute imports or relative imports using a new syntax. This means that absolute imports will be the best way to have something that works now with Python 2 and in the future with Python 3. It is also a bad idea to customize PYTHONPATH or add path modifiers like Leginon.pth as a shortcut to not using absolute paths. In all cases, there is risk of conflicting module names between different packages.
There are a few parts to resolving this in myami:
- Change import statements throughout myami to absolute module names. I think it should be OK to leave alone relative imports if it is a module importing another module in the exact same directory, but this will require syntax revision for Python 3. Scripts in appion/bin should use "import appionlib.xxxxx"
- Other references to module names in myami should start using absolute module names. For instance, the field names in the database like "REF|leginondata|AcquisitionImageData|image".
sinedon.cfg contains just a simple module name rather than absolute name. Noderegistry in Leginon had relative modules, but that has been fixed.
Updated by Neil Voss over 14 years ago
- Target version set to Appion/Leginon 2.1.0
Updated by Amber Herold over 14 years ago
- Priority changed from Normal to Low
- Target version changed from Appion/Leginon 2.1.0 to Appion/Leginon Future Version
- Affected Version set to Appion/Leginon 2.0.1
Updated by Jim Pulokas over 13 years ago
- Show in known bugs set to No
Anchi was getting an exception which I traced to the leginondata module getting imported twice. This can happen if you do
import leginonin one place and
import leginon.leginondatain another place. Python is pretty good about not importing the same module more than once if you import them as the same name. In this case, the module is imported as two different modules, therefore a test such as
leginon.leginondata.SessionData is leginondata.SessionDatareturn "False". The only solution is to be consistent about how a module is imported. As mentioned previously in this issue, the best practice is to use absolute imports. I solved Anchi's problem by converting all relative imports of the leginondata module to absolute imports. See r15944.
Updated by Anchi Cheng about 11 years ago
- Assignee changed from Jim Pulokas to Anchi Cheng