Project

General

Profile

Actions

Bug #67

open

Python imports, absolute vs. relative

Added by Jim Pulokas almost 15 years ago. Updated about 11 years ago.

Status:
Assigned
Priority:
Low
Assignee:
Category:
-
Start date:
01/28/2010
Due date:
% Done:

30%

Estimated time:
Affected Version:
Appion/Leginon 2.0.1
Show in known bugs:
No
Workaround:

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.


Related issues 1 (0 open1 closed)

Related to Appion - Bug #42: Pyappion lib folder and import commandsClosed01/20/2010

Actions
Actions #1

Updated by Neil Voss over 14 years ago

  • Target version set to Appion/Leginon 2.1.0
Actions #2

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
Actions #3

Updated by Jim Pulokas about 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 leginon
in one place and
import leginon.leginondata
in 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.SessionData
return "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.

Actions #4

Updated by Anchi Cheng about 11 years ago

  • Assignee changed from Jim Pulokas to Anchi Cheng
Actions

Also available in: Atom PDF