Python Forum

Full Version: Converting complex codebase from 2.7 to 3.5
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

Output:
Package1 | |- Subpackage1 | |- Subpackage2
Package1 modules import things from Subpackage1 or Subpackage2 by using constructs like these:

from Subpackage1 import module1
import Subpackage1.module2
import Subpackage1.Subpackage2.module3
Similarly, things in Subpackage1 import things from Subpackage2 by doing:

import Subpackage2.module3
from Subpackage2 import module4
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):

import module3
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

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.
There is a site that converts the code to the newest version, not sure about the modules, if it will translate them, it have worked for one code that i had, maybe give it a try.

https://pythonconverter.com/
Sorry, I should have said that this is after running the 2to3 conversion on all the code, so all the code is valid Python 3 already. It's just the import structure which is standing in my way now.

Thanks.
(Apr-18-2019, 09:06 PM)rlgoodman Wrote: [ -> ]Sorry, I should have said that this is after running the 2to3 conversion on all the code, so all the code is valid Python 3 already. It's just the import structure which is standing in my way now.

Thanks.
I found this one, he is asking understanding about that import Z, and they sugered from module import *


https://softwareengineering.stackexchang...ule-import