Use ‘python -m pip’ Everywhere
I’ve just moved all my open source repositories from using plain
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.
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.
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
~/.bash_profile (if one already exists, edit that). If you’re using another shell, check its documentation.
python -m pip,
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.
- ‘pip install’ From a Git Repository
- Why does Python log a SyntaxWarning for ‘is’ with literals?
- Limit Your Try Clauses in Python
Tags: pip, python