6

Eventually, every single time I install a new Linux distribution I do sudo apt-get install python3.

However, once installed I always get confused. python is Python 2.7 and python3 is Python 3.x. But also it appears that pip is for Python 2 and pip3 for Python 3. That said most tutorials I see on Internet always use the traditional pip install even though it is about Python 3.

How should I deal with this? Should I simply continue to put this annoying 3 every time I use Python (pip3, ipython3, python3...)? In most of my lectures I read that creating a symlink python->python3 is a bad practice. Is that correct?

nowox
  • 25,978
  • 39
  • 143
  • 293
  • To add: creating a symlink is dangerous because other programs may expect `python` to be a specific version of python (e.g. 2.7 instead of 3.x) – amdex May 07 '20 at 18:10
  • 2
    There is a related PEP: https://www.python.org/dev/peps/pep-0394 It is about the 'python' naming, but 'pip' naming just mirrors the problem. – VPfB May 07 '20 at 18:15
  • 3
    Does this answer your question? [pip or pip3 to install packages for Python 3?](https://stackoverflow.com/questions/40832533/pip-or-pip3-to-install-packages-for-python-3) – Sabito stands with Ukraine Dec 20 '20 at 10:40
  • For a new Ubuntu 22.04 LTS installation I found only python3 installed but no `pip` or `pip3`. After installing `sudo apt install python3-pip`, `/usr/bin/pip` and `/usr/bin/pip3` appeared. Checking MD5 sums for these, they show as identical: `md5sum /usr/bin/pip3` equals `mdsum /usr/bin/pip` – Richard Logwood Jul 10 '23 at 20:27

6 Answers6

14

Use python3 -m pip or python -m pip. That will use the correct pip for the python version you want. This method is mentioned in the pip documentation:

python -m pip executes pip using the Python interpreter you specified as python. So /usr/bin/python3.7 -m pip means you are executing pip for your interpreter located at /usr/bin/python3.7.

Symlinking python->python3 is a bad idea because some programs might rely on python being python 2. Though, I have seen some Dockerfiles symlink python->python3, like TensorFlow's CPU dockerfile (it's less of an issue in a Docker image). Coincidentally, that same Dockerfile uses the python3 -m pip install syntax that I recommend.

jkr
  • 17,119
  • 2
  • 42
  • 68
  • Why nobody uses `python3 -m pip` in their tutorials? – nowox May 07 '20 at 18:08
  • 3
    because it is commonly understood to refer to mean "pip, in whatever capacity you use it". On my system `pip` points to the python3 pip, as packaged by miniconda. On other systems it might be different – amdex May 07 '20 at 18:11
  • @nowox - i also haven't seen `python3 -m pip` used much, but the pip documentation does include it: https://pip.pypa.io/en/stable/user_guide/#running-pip – jkr May 07 '20 at 18:12
4

Whether the correct command for Python 3 is pip or pip3 or (say) gaschplutzga depends on a number of factors.

If you only have Python 3, and you have a command named pip, that's probably safe to use. Going forward, this will be the simple, obvious, safe answer in more and more places.

If you have both Python 2 and Python 3, and there is a command called pip3 installed on your system, probably that's the correct one to use.

More generally, you can go through your PATH and look for commands with suitable names. On Unix-like systems with a POSIX-compatible shell, try the commands command -V pip3 and command -V pip. (On Windows systems, maybe try where pip3 and where pip, or pray to whatever dark deity informed your choice of operating system.)

If you receive output like

/opt/random/nonstandard/whoa/pip
/usr/local/bin/pip
/usr/bin/pip

you can try each of these in turn with the full path and adding the --version option to have them identify themselves. When you specify the full path, you are bypassing the system's PATH mechanism entirely. For example,

/opt/random/nonstandard/whoa/pip --version

might identify itself as belonging to Python version 3.2.1. If that's the one you want, and it's at the top of your PATH, you can simply rely on the PATH to give you this version when you type just pip. If not, perhaps you can shuffle your PATH (but understand that this changes the resolution order for all commands in the directory whose position you change) or create a simple alias or wrapper which bypasses the PATH for this particular command in your personal account. On Unix-like systems with a POSIX-compatible shell, this might look like

alias pip=/opt/random/nonstandard/whoa/pip

(to persist this across sessions, you'd add this to your .profile or similar - for Bash, try .bash_profile if it exists; for Zsh, try .zshrc. The full scoop for each shell is more complicated than I can squeeze into these narrow parentheses); on Windows, you might be able to control this by setting the environment variable PY_PYTHON, but there's a huge can of worms behind that "might".

Some sites and OSes / distros have additional wrappers or conventions which introduce additional options; if you use a specific package manager, perhaps also study its documentation. (One common example is Anaconda, though I don't believe it affects the naming or location of pip specifically.)

One common arrangement is to use python -m pip instead of the bare pip command; this reasonably expects that whichever python version you run will be able to figure out how to find its specific version of pip from its module library. Everything about how to select a specific pip binary above also applies to the logic for finding a specific python binary; so (as long as it isn't pathologically misconfigured) /opt/random/nonstandard/whoa/python -m pip will run the correct pip version for that Python version. You could argue that this only pushes the problem to a different corner, but it's convenient if you have multiple Python versions to have a simple way to select the correct pip for each with a simple, unified mechanism.

On Windows, probably use py -m pip and expect the py wrapper to select the correct Python binary. The py command offers convenience functionality to select either Python 2 or Python 3 if both are installed. Going forward, there are plans to make this wrapper available on other platforms, too.

tripleee
  • 175,061
  • 34
  • 275
  • 318
3

creating a symlink python->python3 is a bad practice. Is that correct?

Sometimes. Some OSs (looking at you, macOS) deeply rely on python pointing to a Python 2 interpreter for internal tools and tasks. Deleting the shipped Python 2 interpreter (or aliasing python to a Python 3 interpreter) will break stuff. How to uninstall Python 2.7 on a Mac OS X 10.6.4?

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • For the record, this is no longer true. As of MacOS Ventura (v 13) the OS no longer requires or even offers an option to install Python 2 as part of the system install. Increasingly, the same is true for any Linux distro which previously had system tools which depended on Python 2. – tripleee Jul 03 '23 at 07:36
1

Use virtual environments, then pip would be associated with the python used to create that virtual environment. Whether you use pip or pip3, it will be equivalent to python3 -m pip as mentioned in jakub's answer. Also, given that Python 2.7 is already EOL (which means you will most likely work with Python 3) and that pip install-ing things onto the system packages should be avoided, then a virtual environment would be helpful here.

For example, using pipenv:

$ pipenv --python=/usr/local/opt/python@3.8/bin/python3

$ pipenv shell
Launching subshell in virtual environment...

(TEMP) $ pip --version
pip 20.2.3 from /Users/me/.venvs/temp2-SbXvZiFd/lib/python3.8/site-packages/pip (python 3.8)
(TEMP) $ pip3 --version
pip 20.2.3 from /Users/me/.venvs/temp2-SbXvZiFd/lib/python3.8/site-packages/pip (python 3.8)

For example, using venv:

$ python3.8 -m venv .venv
$ source .venv/bin/activate

(.venv) $ pip --version
pip 20.2.3 from /Users/me/temp2/.venv/lib/python3.8/site-packages/pip (python 3.8)
(.venv) $ pip3 --version
pip 20.2.3 from /Users/me/temp2/.venv/lib/python3.8/site-packages/pip (python 3.8)

The virtual environment takes care of making sure pip or pip3 in this env refers to the pip from the correct Python version. You can then happily follow tutorials that still use pip install something (unless of course that tutorial refers to a Python 2.7 or a system-wide installation).

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
0

You can install pip through pip3 and this should resolve this issue.

$ pip --version
pip 19.0.3 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

Notice that pip here is of Python 2.7 (in this example).

You can then force pip3 of Python 3.X to install pip under itself.

$ sudo pip3 install pip --upgrade
Installing collected packages: pip
  Found existing installation: pip 8.1.1
    Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-19.0.3

Once you check this again, it should reference Python 3.X so you don't have to deal with what is what.

$ pip --version
pip 19.0.3 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

I doubt you'll want to use Python 2 after this, but if you do happen to work with Python 2 code, you can create a virtual environment to access those commands again. Otherwise, you won't have to worry about the pip or pip3 distinction after this.

Not really a duplicate of this question, but this helped me suggest this answer: Can pip (python2) and pip3 (python3) coexist?

Eric Leung
  • 2,354
  • 12
  • 25
  • If you run `which -a pip`, you will see that both python2 and python3 `pip` still exist, so finding the correct `pip` is a matter of `PATH` configuration. – jkr May 07 '20 at 18:22
  • (The `which` command is nonstandard; yours might not exist, or might not support an `-a` option. The POSIX standard command is `command -V`, and/or `type`.) – tripleee Jul 03 '23 at 07:42
0

Pip is for python version less than 3. and pip3 is used when you want to install packages for python version 3 or higher.