Pip or Pip3 to Install Packages for Python 3

pip or pip3 to install packages for Python 3?

Your pip is a soft link to the same executable file path with pip3.
you can use the commands below to check where your pip and pip3 real paths are:

$ ls -l `which pip`
$ ls -l `which pip3`

You may also use the commands below to know more details:

$ pip show pip
$ pip3 show pip

When we install different versions of python, we may create such soft links to

  • set default pip to some version.
  • make different links for different versions.

It is the same situation with python, python2, python3

More information below if you're interested in how it happens in different cases:

  • MacOS/Homebrew
  • Fedora/CentOS
  • Debian/Ubuntu

Should I use pip or pip3?

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.

How to install pip with Python 3?

edit: Manual installation and use of setuptools is not the standard process anymore.

If you're running Python 2.7.9+ or Python 3.4+

Congrats, you should already have pip installed. If you do not, read onward.

If you're running a Unix-like System

You can usually install the package for pip through your package manager if your version of Python is older than 2.7.9 or 3.4, or if your system did not include it for whatever reason.

Instructions for some of the more common distros follow.

Installing on Debian (Wheezy and newer) and Ubuntu (Trusty Tahr and newer) for Python 2.x

Run the following command from a terminal:

sudo apt-get install python-pip 

Installing on Debian (Wheezy and newer) and Ubuntu (Trusty Tahr and newer) for Python 3.x

Run the following command from a terminal:

sudo apt-get install python3-pip
Note:

On a fresh Debian/Ubuntu install, the package may not be found until you do:

sudo apt-get update

Installing pip on CentOS 7 for Python 2.x

On CentOS 7, you have to install setup tools first, and then use that to install pip, as there is no direct package for it.

sudo yum install python-setuptools
sudo easy_install pip

Installing pip on CentOS 7 for Python 3.x

Assuming you installed Python 3.4 from EPEL, you can install Python 3's setup tools and use it to install pip.

# First command requires you to have enabled EPEL for CentOS7
sudo yum install python34-setuptools
sudo easy_install pip

If your Unix/Linux distro doesn't have it in package repos

Install using the manual way detailed below.

The manual way

If you want to do it the manual way, the now-recommended method is to install using the get-pip.py script from pip's installation instructions.

Install pip

To install pip, securely download get-pip.py

Then run the following (which may require administrator access):

python get-pip.py 

If setuptools is not already installed, get-pip.py will install setuptools for you.

Should I use pip or pip3? (clarification)

You have asked a lot of questions... here are your answers:

Q. How you can tell if you use the first one or the other?

A. Use the command pip --version and pip3 --version.

Q. Is it the same as using pip3 or pip?

A. Yes, if both of the above commands gives the same result; no, if otherwise.

Q. Now I am mostly using python3 over python, does it mean that packages installed via pip are now useless?

A. Yes, python2 and python3 aren't backwards compatible, all the new code that you are writing doesn't work with python2; however, be careful while deleting stuff, you could break some code somewhere due to accident.

Q. If there is advice for some package to install it by: pip install "some package" does it actually mean: use pip for the python version you use?

A. Refer to answer 1.

Q. Is it possible that single python program imports python(2) and python3 packages at the same time?

A. No, it doesn't work that way. Check the links in the comments.

python3 -m pip install VS pip3 install

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

  • Brett Cannon's article "Why you should use python -m pip"
  • pip's documentation section on "Upgrading pip"
  • venv's documentation section on "Creating virtual environments": "You don’t specifically need to activate an environment [...]"

Pip for Python 3.8

Install pip the official way:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.8 get-pip.py

made 3.8 my default Python version

It depends on how you did that, but it might break something in your OS. For example some packages on Ubuntu 18.04 might depend on python being python2.7 or python3 being python3.6 with some pip packages preinstalled.

pip install vs python3 -m pip install

On many systems, pip uses the Python 2 interpreter, while pip3 uses the Python 3 interpreter. When only Python 3 is installed, pip is identical to pip3. One way to know which interpreter is used, is to read the first line of the file pip.

python3 -m pip install is equivalent to pip3 but at least it's explicit that you want to use python3.

In your case, it looks like pip uses the Python 2 interpreter but with the module that has been installed for Python 3. That's curious.

I'd recommend you to use python3 -m pip install instead of relying on the command pip.

How to use pip with Python 3.x alongside Python 2.x

The approach you should take is to install pip for Python 3.2.

You do this in the following way:

$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python3.2 get-pip.py

Then, you can install things for Python 3.2 with pip-3.2, and install things for Python 2-7 with pip-2.7. The pip command will end up pointing to one of these, but I'm not sure which, so you will have to check.



Related Topics



Leave a reply



Submit