I'm having some trouble with latest python version (2 & 3) to find a good common way for imports.
About the packages I pushed on PyPI, that's ok yet as I create a subfolder for the package, then use package.module
in __init__.py
for imports.
I would do the same in module files.
Other than that, I have some packages that I share between projects (stored on my svn server) which I import as svn external in projects.
The hierarchy is usually designed this way:
package:
|- __init__.py
|- module1.py
|- module2.py
|- ...
When I import it into projects, folders looks like this:
project:
|- package:
|- what's above
|- app.py
I usually develop the package itself as a project, meaning package
is root.
When I import it into projects, they become packages, thus not at root anymore.
When using package.module
or .module
in standalone package project, of course, this does not work.
When using same as package in other projects, this tends to work almost fine, but I experience some issues with Python 3.6, telling that some names aren't defined (when launched directly from the command line; but working under pycharm).
Last but not least, when using py2exe to make executables (from Python 2.7), I get some undefined names (classes, etc) exceptions even if imports seems to be ok as no such exception is raised; please note I was able to see that related .pyc files are there in library.zip.
I did not go through these issues some months ago, neither python 2 nor python 3 with older versions.
I open this question as I searched on StackOverflow, python documentation (2 & 3) and a lot through the web these last days without finding something that is really relevant to the overall issue.
My question is, does anyone have any clue on a good practice for imports that would be compatible between python 2 & 3, and which would work also when package is __main__
? And the bonus, still ok when packed with py2exe?
At some point, i tried some try/except ImportError
blocks for these imports, but it messes things up a bit & seems to be unreliable.
Thanks a lot for your help!