[Solved] Import two modules with same name at top of PYTHONPATH elements


This import statement is incorrect:

from utils.pkg2.mod2 import func2

If it has ever worked correctly, that was relying on resolving with the current working directory, implicit relative imports in Python 2.x, or a manually munged PYTHONPATH / sys.path.

This is the type of import for which PEP8 said:

Implicit relative imports should never be used and have been removed in Python 3.

So what to do instead? sys.path should be augmented with top-level directories, not intra-package directories, i.e.:

PYTHONPATH=/path1/dir1:/path2

And change imports like this:

from dir2.utils.pkg2.mod2 import func2

Now the sub-package dir2.utils is namespaced from the top-level package utils.

1

solved Import two modules with same name at top of PYTHONPATH elements