I’ve been familiar with Python’s -m
flag for a while but never had quite internalized what it was really doing.
While reading about this cool AI pair programming project called aider
, the docs mentioned that the tool could be invoked via python -m aider.main
“[i]f your pip install did not place the aider executable on your path”.
I hadn’t made this association between pip installed executables and the -m
flag.
The source for the file that runs when that Python command is invoked can be found here.
I tried running the following in a project that had the llm
tool installed and things began to make more sense
โฏ python -m llm "test"
It looks like you're testing the system. How can I assist you further?
Looking at the source, we can find the invocation point in __main__.py
.
Following up with Claude, I learned
The
-m
flag tells Python to run a module as a script. It searches for the specified module in the Python module search path and executes its contents as the main module.
This is response is almost verbatim from the Python docs.
Another use of -m
I’ve commonly seen is
python -m http.server
For completeness, here is that source code. Now, I have a better sense of what is really going on.