How not to run a script in Python
Redowan Delowar
March 16, 2022
When I first started working with Python, nothing stumped me more than how bizarre Python's
import system seemed to be. Often time, I wanted to run a module inside of a package with
the python src/sub/module.py command, and it'd throw an ImportError that didn't make any
sense. Consider this package structure:
Let's say you're importing module a in module b:
Now, if you try to run module b.py with the following command, it'd throw an import error:
What! But you can see the src/a.py module right there. Why can't Python access the module
here? Turns out Python puts the path of the module that you're trying to access to the top
of the sys.path stack. Let's print the sys.path before importing module a in the
src/sub/b.py file:
Now running this module with python src/sub/b.py will print the following:
From the first section of the above output, it's evident that Python looks for the imported
module in the src/sub/ directory, not in the root directory from where the command is
being executed. That's why it can't find the a.py module because it exists in a directory
above the sys.path's first entry. To solve this, you should run the module with the -m
switch as follows:
This will not raise the import error and return the following output:
Here, the first entry denotes the root directory from where the script is being run from.
Voila, problem solved!
Further reading
- [Don't run python my/script.py]
[don't run python my/script.py]:
https://www.youtube.com/watch?v=hgCVIa5qQhM
Discussion in the ATmosphere