1

I am very new to Python programming. I can use pip install without any trouble. I've seen a lot of posts in online which prefix pip install with python -m. When I use python -m before the pip command nothing seems to happen. Please explain.

1 Answer 1

2

If you run python -h, the -m flag is described as:

-m mod : run library module as a script (terminates option list)

In this case, when you run python -m pip Python runs the pip module as a script (i.e. it executes the code inside it). If you have added pip to your PATH you can then just run pip directly without the need to invoke it via python.

python -m pip install <some module> and pip install <some module> should perform identically. If you have multiple versions of Python installed on your system, using pip might install a module for the wrong version of Python. You can check the version using pip -V which will output something like

pip 18.1 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)

here the python version is python3.7.

If the version of the pip command doesn't match with the version of Python used to execute a program when the program is executed it will not run. You can check your Python version with python -V, which will output something like

Python 2.7.16

If the pip version doesn't match your Python version you can either update your path's pip, or you can use python -m pip. To select the correct version (i.e. if python runs Python 2 and you want to use Python 3) just execute python3 -m pip or python3.7 -m pip.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.