Meaning of python -m flag
Solution 1:
From Python Docs:
Since the argument is a module name, you must not give a file extension (.py). The
module-name
should be a valid Python module name, but the implementation may not always enforce this (e.g. it may allow you to use a name that includes a hyphen).Package names are also permitted. When a package name is supplied instead of a normal module, the interpreter will execute
<pkg>.__main__
as the main module. This behaviour is deliberately similar to the handling of directories and zipfiles that are passed to the interpreter as the script argument.
Solution 2:
If you type python --help
You get
// More flags above
-m mod : run library module as a script (terminates option list)
// and more flags below
A great many things in a terminal will show you how to use it if you either use command --help
or man command
Solution 3:
The -m
stands for module-name
.
From Command line and environment:
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
Solution 4:
Consider the following scenario.
You have three versions of Python installed:
- Python 3.7
- Python 3.8
- Python 3.9
Your "default" version is 3.8. It's the first one appearing in your path. Therefore, when you type python3
(Linux or Mac) or python
(Windows) in a shell you will start a 3.8 interpreter because that's the first Python executable that is found when traversing your path.
Suppose you are then starting a new project where you want to use Python 3.9. You create a virtual environment called .venv
and activate it.
python3.9 -m venv .venv # "py -3.9" on Windows
source .venv/bin/activate # ".venv\Scripts\activate" on Windows
We now have the virtual environment activated using Python 3.9. Typing python
in a shell starts the 3.9 interpreter.
BUT, if you type
pip install <some-package>
Then what version of pip
is used? Is it the pip for the default version, i.e. Python 3.8, or the Python version within the virtual environment?
An easy way to get around that ambiguity is simply to use
python -m pip install <some-package>
The -m
flag makes sure that you are using the pip that's tied to the active Python executable.
It's good practice to always use -m
, even if you have just one global version of Python installed from which you create virtual environments.
Re. path
The so-called path is a list of directories where your system searches for executables. When you type a command, like python
, this list is traversed from the first directory to the last, searching for a filename that matches the command you typed.
If the filename/command is found, the matched file gets executed without taking into account potential later matches. If no match occurs, you get a Command not found
or a variation thereof. This behavior is by design.
On UNIX systems the path environment variable is called $PATH
, while on Windows systems it's referred to as %PATH%
Solution 5:
When -m
is used with a python
statement on the command line, followed by a <module_name>
, then it enables the module to be executed as an executable file.
You can refer to python docs for the same, or run python --help