1

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!

SMFSW
  • 286
  • 1
  • 2
  • 10

1 Answers1

1

Given some clues about __package__ from:

Relative imports for the billionth time

I found some more consistent solution to check before import.

if __package__ is None or __package__ == '':
    from module1 import *
    from module2 import *
else:
    from .module1 import *
    from .module2 import *

In case you run directly from the file itself (let's say module1), __package__ is None. I still had to add check for __package__ == '' as if module1 imports module2, package is not None in module2.

In this case, I prefer using relative imports even if not really recommended, as when used as a package, the package itself can have any name without changing anything.

About __init__.py file, I just use relative imports as it will be used only when importing as a package from the main app with:

import package
from package import xxx

This seems to solve things well about imports for both python 2.7.14 & 3.6, yet still causes issues with py2exe (2.7) and doesn't generate the whole poject.

SMFSW
  • 286
  • 1
  • 2
  • 10