Adam Johnson

Home | Blog | Training | Projects | Colophon

Use 'python -m pip' Everywhere

2020-02-25

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.

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.


Interested in Django or Python training? I'm taking bookings for workshops.


Subscribe via RSS, Twitter, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: python