Import a module from both within same package and from outside the package in Python 3

TLDR:

  • Run your code with python -m pkg.mod2.
  • Import your code with from . import mod1.

The only “solution”, I’ve found is to go up one folder and do python -m pkg.mod2 and then it works.

Using the -m switch is indeed the “only” solution – it was already the only solution before. The old behaviour simply only ever worked out of sheer luck; it could be broken without even modifying your code.

Going “one folder up” merely adds your package to the search path. Installing your package or modifying the search path works as well. See below for details.

But do we have to be adding the package prefix pkg to every import to other modules within that package?

You must have a reference to your package – otherwise it is ambiguous which module you want. The package reference can be either absolute or relative.

A relative import is usually what you want. It saves writing pkg explicitly, making it easier to refactor and move modules.

# inside mod1.py
# import mod2 - this is wrong! It can pull in an arbitrary mod2 module
# these are correct, they uniquely identify the module
import pkg.mod2
from pkg import mod2
from . import mod2
from .mod2 import foo  # if pkg.mod2.foo exists

Note that you can always use <import> as <name> to bind your import to a different name. For example, import pkg.mod2 as mod2 lets you work with just the module name.

Even more, to run any scripts inside the package, do I have to remember to go one folder up and use the -m switch? That’s the only way to go??

If your package is properly installed, you can use the -m switch from anywhere. For example, you can always use python3 -m json.tool.

echo '{"json":"obj"}' | python -m json.tool

If your package is not installed (yet), you can set PYTHONPATH to its base directory. This includes your package in the search path, and allows the -m switch to find it properly.

If you are in the executable’s directory, you can execute export PYTHONPATH="$(pwd)/.." to quickly mount the package for import.

I’m confused. This scenario was pretty straightforward with python 2, but looks awkward in python 3.

This scenario was basically broken in python 2. While it was straightforward in many cases, it was difficult or outright impossible to fix in any other cases.

The new behaviour is more awkward in the straightforward case, but robust and reliable in any case.

Leave a Comment