python3 -m pip install VS pip3 install

Question:

I always use pip install (which I think is equivalent to pip3 install since I only have python3 in my env) to install packages. But I recently heard python3 -m pip install is better. Why?

Asked By: F.S.

||

Answers:

It’s the same thing.

python3 -m pip install calls pip as a module in python, while pip install calls pip directly.

The only reason to prefer the first is that in order to use the second you need to have set pip in your environmental variables (for Windows). In older versions of python this was not done automatically during installation, rather you had to do this manually. That’s why in a lot of guides you might see them using the first syntax for their instructions (because it works always, as long as you have python3 in your environmental variables. For Linux/Mac operating systems there isn’t any difference.

Answered By: Djib2011

I would advise against ever calling any pip somecommand (or pip3) script directly. Instead it’s much safer to call pip‘s executable module for a specific Python interpreter explicitly, something of the form path/to/pythonX.Y -m pip somecommand.

There are many advantages to this, for example:

  • It is explicit for which Python interpreter the projects will be pip-installed (Python 2 or 3, inside the virtual environment or not, etc.)
  • For a virtual environment, one can pip-install (or do other things) without activating it: path/to/venv/bin/python -m pip install SomeProject
  • Under Windows this is the only way to safely upgrade pip itself pathtovenvScriptspython.exe -m pip install --upgrade pip

But yes, if all is perfectly setup, then python3 -m pip install SomeProject and pip3 install SomeProject should do the exact same thing, but there are way too many cases where there is an issue with the setup and things don’t work as expected and users get confused (as shown by the many questions about this topic on this platform).


References

Answered By: sinoroc
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.