Should I Use Pip Or Pip3?
Solution 1:
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
executespip
using the Python interpreter you specified aspython
. So/usr/bin/python3.7 -m pip
means you are executingpip
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.
Solution 2:
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?
Solution 3:
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, 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.)
Solution 4:
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).
Solution 5:
You can install pip
throughpip3
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?
Post a Comment for "Should I Use Pip Or Pip3?"