Absolute imports in python not working, relative imports work

Since it’s not shown, I have to assume you’re running python a/foo.py, this puts the directory of the script ('a') on the beginning of sys.path when in reality you want the current directory on the beginning of sys.path.

You should instead run python -m a.foo which will correctly initialize the sys.path roots for your project structure.

Here’s a more in-depth writeup that I wrote on other ways this same problem can go pearshaped.

Additionally I recorded a video on the topic: don’t run python my/script.py!

Leave a Comment