12

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?

F.S.
  • 1,175
  • 2
  • 14
  • 34
  • There is a similar question https://stackoverflow.com/questions/56940665/pip-install-vs-python3-m-pip-install, but it's more like a debugging request. His issue is "pip uses the Python 2 interpreter but with the module that has been installed for Python 3" – F.S. Mar 20 '20 at 23:15

2 Answers2

9

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 path\to\venv\Scripts\python.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

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • Is it safe to conclude that on a mac/unix system, if only one version of python is installed in an env, it is safe to use `pip install`? – F.S. Mar 21 '20 at 19:46
  • Same rules apply. It should be safe, but there are always cases where somehow something went wrong and things don't work as expected. Could be that the environment isn't correctly activated, that `pip`'s _shebang_ has been corrupted, etc. In the end the `pip` script is just a shortcut, so in an interactive context (for example on the command line), where a user is able to check beforehand that the setup is correct and monitor the output and notice potential inconsistencies, it's safe. In other contexts (bash script, _tox_ config, etc.) better be explicit and use the expanded version. – sinoroc Mar 21 '20 at 21:21
  • I think, your `SomeProject`s should be `SomePackage`. – muyustan Mar 22 '20 at 15:13
  • @muyustan I use this terminology on purpose to stay away from the confusion between _distribution packages_ and _import packages_. Neither of those are the right term for what one should give to _pip_. `SomeProject` is a project published on PyPI. It may have multiple releases. There may be multiple distributions (_sdist_, _wheel_) for each version released. Each distribution may contain multiple importable top-level modules or packages. -- https://packaging.python.org/glossary/#term-distribution-package -- I tried to (poorly) sum it up here: https://sinoroc.gitlab.io/kb/python/packaging.html – sinoroc Mar 22 '20 at 17:39
  • @sinoroc I guess I messed with the wrong guy :) I don't have that much of information on these topics, your links looks nice, i will inspect them. – muyustan Mar 22 '20 at 17:42
  • @sinoroc your link looks awesome. You are clearly an expert on this topic. As a newbie I'd appreciate namedrop some wellknown package/projects to illustrate your point. For example I'm curious when `a Python project can contain multiple top-level packages` – F.S. Mar 25 '20 at 07:18
  • 1
    @Chris Yes, some examples couldn't hurt. In this case the [_setuptools_ project](https://pypi.org/project/setuptools/) comes to mind. It has 2 _top-level packages_: `setuptools`, and `pkg_resources` (and apparently also 1 _top-level module_: `easy_install`). – sinoroc Mar 25 '20 at 12:08
3

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.

Djib2011
  • 6,874
  • 5
  • 36
  • 41