Using Python's -m Flag for Modules | Generated by AI
Yes, -m
means run a module as a script. Let’s break this down with your examples:
python -m unittest
This tells Python:
- Look up the
unittest
module (which is part of the standard library). - Run it as if it were a script (that’s what
-m
does). -
That’s why you can do things like:
python -m unittest test_example.py python -m unittest discover
This works even though you don’t have a standalone
unittest
file in your project — it’s a standard library module.
python -m pip
This is the same pattern:
- It looks up the
pip
module (installed with Python). - Runs it as if it were
pip
executable. - So
python -m pip install requests
is the “official” safe way to run pip, because it ensures you’re using the same Python interpreter’s pip that matches yourpython
. (Sometimespip
on your system might be pointing to a different Python installation, sopython -m pip
avoids confusion.)
Difference from python test_xx.py
When you do:
python test_xx.py
you are running that specific file directly, not using unittest
’s test discovery or runner.
- If
test_xx.py
has aunittest.main()
at the bottom, it will run tests. - But if you want Python to automatically discover all tests under a directory, you usually rely on
python -m unittest discover
.
✅ Summary:
-m
= run a module as a script.python -m unittest
runs the standard library’s test runner.python -m pip
ensures pip corresponds to the same Python interpreter.python test_xx.py
just runs one file, not the general test runner.
Would you like me to also show you how pytest handles this differently, since you might compare unittest
vs pytest
?