Apr-18-2019, 08:11 PM
Hello smart Python people,
I have been working with this code for about 12 years now and we are finally getting around to converting to Python 3.5 (Please don't remind me how late we are!)
I have a number of packages which are built in a tree. A simple visualization:
Similarly, things in Subpackage1 import things from Subpackage2 by doing:
However, each package and subpackage is also a project in its own right, and each has lots of modules which are run as __main__ modules, and import the other modules from within their own package by simply doing (for instance, for a main module in Subpackage2):
The problem comes now that I am running this code using Python 3, the subpackage imports do not work as absolute imports.
For instance, there is a main module in Subpackage1 called X, which imports a module from Subpackage2 called Y. This module in turn imports Z also from Subpackage2. When I run the code, this is the error I get:
Subpackage1 $ python3 X.py
My question, and thank you for reading this far, is this: What is the best way to convert this code, keeping in mind the following:
I would certainly prefer not to have to move the modules which are run as main modules out of each directory, but is that the only way that I can get around this problem?
Thanks in advance for any advice you can give.
I have been working with this code for about 12 years now and we are finally getting around to converting to Python 3.5 (Please don't remind me how late we are!)
I have a number of packages which are built in a tree. A simple visualization:
Output:Package1
|
|- Subpackage1
|
|- Subpackage2
Package1 modules import things from Subpackage1 or Subpackage2 by using constructs like these:1 2 3 |
from Subpackage1 import module1 import Subpackage1.module2 import Subpackage1.Subpackage2.module3 |
1 2 |
import Subpackage2.module3 from Subpackage2 import module4 |
1 |
import module3 |
For instance, there is a main module in Subpackage1 called X, which imports a module from Subpackage2 called Y. This module in turn imports Z also from Subpackage2. When I run the code, this is the error I get:
Subpackage1 $ python3 X.py
Error:Traceback (most recent call last):
File "X.py", line 9, in <module>
import Subpackage2.Y as Y
File ".../Subpackage1/Subpackage2/Y.py", line 18, in <module>
import Z
ImportError: No module named 'Z'
Even though the Z module certainly does exist in Subpackage2. I believe this is because everything is using absolute imports, and the final import winds up looking in Subpackage1 (my default directory) for the Z module. (Is this correct?)My question, and thank you for reading this far, is this: What is the best way to convert this code, keeping in mind the following:
- Each package and subpackage has its own codebase which is run from that directory
- Each package only imports from subpackages below it
- Everything is absolute imports today
I would certainly prefer not to have to move the modules which are run as main modules out of each directory, but is that the only way that I can get around this problem?
Thanks in advance for any advice you can give.