Use ‘python -m pip’ Everywhere

I’ve just moved all my open source repositories from using plain pip
to python -m pip
, in test scripts and documentation.
This is following Brett Cannon’s article from November last year Why you should use python -m pip
, and Donald Stufft’s 4.5 year old issue on the Pip repo itself. Both describe in depth the problems that using the pip
executable can lead to, and are well worth a read.
In brief, the main problem:
Most Python developers install more than one Python version. This is either due to their OS including an old Python by default, working on projects that use different versions, or simple experimentation.
Running the pip
executable with more than one Python version can be unpredictable. When you upgrade pip on one version, it can replace the executable from the other. So running pip install
at one moment might use Python 2.7, then later it could use Python 3.7.
It’s not hard to see how this can lead to problems understanding what’s installed where and when.
When you use python -m pip
, the pip package is executed explicitly with the version of Python that python
points to. If you have other interpreters, you can use them precisely and predictably, e.g. python3.6 -m pip
.
More typing, but much less trouble.
Alias
Update (2020-04-11): Added this section.
If you want to always use python -m pip
, but don’t like the typing, no worries. You can use a shell alias to make pip
map to it. For example, I use ZSH and in my ~/.zshrc
I have:
alias pip='python -m pip'
This way, when I type pip install django
, it’s replaced with python -m pip install django
.
The file to place this in depends on your shell. On Bash it’s either ~/.bashrc
or ~/.bash_profile
(if one already exists, edit that). If you’re using another shell, check its documentation.
Fin
Enjoy using python -m pip
,
—Adam
P.S. python -m pip
was actually my entry in the “Is there a Dr. on this flight?” Twitter meme.
If your Django project’s long test runs bore you, I wrote a book that can help.
One summary email a week, no spam, I pinky promise.
Related posts: