![]() |
Converting complex codebase from 2.7 to 3.5 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Converting complex codebase from 2.7 to 3.5 (/thread-17640.html) |
Converting complex codebase from 2.7 to 3.5 - rlgoodman - Apr-18-2019 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: Package1 modules import things from Subpackage1 or Subpackage2 by using constructs like these:from Subpackage1 import module1 import Subpackage1.module2 import Subpackage1.Subpackage2.module3Similarly, things in Subpackage1 import things from Subpackage2 by doing: import Subpackage2.module3 from Subpackage2 import module4However, 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 module3The 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 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:
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. RE: Converting complex codebase from 2.7 to 3.5 - francisco_neves2020 - Apr-18-2019 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/ RE: Converting complex codebase from 2.7 to 3.5 - rlgoodman - Apr-18-2019 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. RE: Converting complex codebase from 2.7 to 3.5 - francisco_neves2020 - Apr-18-2019 (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.I found this one, he is asking understanding about that import Z, and they sugered from module import * https://softwareengineering.stackexchange.com/questions/345067/from-module-import-x-y-z-vs-from-module-import |