python -m flag

Foong Min Wong
1 min readMar 28, 2024

I was testing one of the Python scripts in a custom library by running python graphutils/plot.py one level up from the folder. It threw an error: ImportError: attempted relative import with no known parent package.

Then I ran python -m graphutils.plot to test the .py script in that folder, and it works! Instead of specifying the path to your Python script, you can run a script with dot notation if you are not in the script folder directory.

-m mod: run library module as a script (PEP 338)

When you have a Python script that comes with this line if __name__ == “__main__”: and you run the script with the -m flag afterward, it will import the relevant module or package (in this case, graphutils), then execute the stuff after that line.

In the command line, the -m flag allows code execution via the module name rather than the filename when the script contains relative imports without installing them.

--

--