What does -m in python -m pip install <package>
mean ?
or while upgrading pip using python -m pip install --upgrade pip
.
-
7It's how you can run a module as a script.– jedwardsJun 12, 2018 at 15:56
-
25@Jean-FrançoisFabre IMO that is not a good reason to close, many high voted basic questions are very easy to find in the documentation or manual. Anyway +0– Chris_RandsJun 12, 2018 at 16:04
-
2Possible duplicate of What is the purpose of the -m switch?– phdJun 12, 2018 at 20:24
-
1stackoverflow.com/questions/22241420/…– phdJun 12, 2018 at 20:24
-
5"Read the manual" is such an unempathetic answer. For people who know this is a basic question that can be found in the manual that sounds easy - the problem is, when you are learning, you cannot tell the difference between things that should be easy to find and things that are complicated.– Hector OrdonezSep 7, 2021 at 9:21
6 Answers
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.
-
2so this just runs the python script as a normal executable? I don't understand when I'd use the
-m
flag. My scripts always work when I run them without it. Apr 23, 2021 at 17:21 -
2I'm not sure what the difference in this case btw a module and a script but I find the comment:
It's how you can run a module as a script.
python.org/dev/peps/pep-0338 to be interesting. Though the main question still remains what is a script and a module and why the-m
is really needed. Feb 3 at 19:33
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%
The -m
stands for module-name
.
From Command line and environment:
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
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
-
79The main question remains unanswered, what does it mean "to run as a script". Aug 25, 2020 at 18:35
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
-
11so this just runs the python script as a normal executable? I don't understand when I'd use the
-m
flag. My scripts always work when I run them without it. Apr 23, 2021 at 17:22 -
@CharlieParker From what I understand, the
-m
flag is useful because it will search for a module/package on all paths avaible insys.path
instead of just the current working directory. Feb 1 at 22:29 -
-
@CharlieParker not really, if you try
python timeit
in your terminal and there isn't a file/folder namedtimeit
in your current working directory you'll get aNo such file or directory
error, but if you add the-m
flag then python will run the timeit package that comes with the standard library Feb 18 at 13:42
If you have multiple versions of python installed and you want to upgrade pip pip install --upgrade pip
how do you know which python version will be affected? it depends on path variable for the shell. You might also get warning in this case. To avoid this confusion use -m
then it looks in variable sys.path. This is another advantage of -m
.
# importing module
import sys
# printing all directories for
# interpreter to search
sys.path