What does -m in python -m pip install <package> mean ?
or while upgrading pip using python -m pip install --upgrade pip.
- 1,515
- 2
- 7
- 9
-
7It's how you can run a [module as a script](https://www.python.org/dev/peps/pep-0338/). – jedwards Jun 12 '18 at 15:56
-
26@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_Rands Jun 12 '18 at 16:04
-
2Possible duplicate of [What is the purpose of the -m switch?](https://stackoverflow.com/questions/7610001/what-is-the-purpose-of-the-m-switch) – phd Jun 12 '18 at 20:24
-
1https://stackoverflow.com/questions/22241420/execution-of-python-code-with-m-option-or-not – phd Jun 12 '18 at 20:24
-
disagree with closing. closing defeats the purpose and it is a good question. Unfortunately the answers are not that good – KansaiRobot Feb 23 '20 at 04:08
-
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 Ordonez Sep 07 '21 at 09:21
-
I'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.` https://www.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. – Charlie Parker Feb 03 '22 at 19:33
6 Answers
From Python Docs:
Since the argument is a module name, you must not give a file extension (.py). The
module-nameshould 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. – Charlie Parker Apr 23 '21 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.` https://www.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. – Charlie Parker Feb 03 '22 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%
- 2,425
- 2
- 21
- 23
The -m stands for module-name.
From Command line and environment:
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
- 386,424
- 207
- 554
- 861
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
- 2,864
- 4
- 31
- 47
-
80The main question remains unanswered, what does it mean "to run as a script". – Serge Mosin Aug 25 '20 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
- 14,460
- 16
- 79
- 88
- 303
- 3
- 7
-
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. – Charlie Parker Apr 23 '21 at 17:22
-
1@CharlieParker From what I understand, the `-m` flag is useful because it will search for a module/package on all paths avaible in `sys.path` instead of just the current working directory. – qwerty_url Feb 01 '22 at 22:29
-
-
@CharlieParker not really, if you try `python timeit` in your terminal and there isn't a file/folder named `timeit` in your current working directory you'll get a `No 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 – qwerty_url Feb 18 '22 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
- 5,849
- 2
- 44
- 78