Question

Is there any way to make pip play well with multiple versions of Python? For example, I want to use pip to explicitly install things to either my site 2.5 installation or my site 2.6 installation.

For example, with easy_install, I use easy_install-2.{5,6}.

And, yes — I know about virtualenv, and no — it's not a solution to this particular problem.

Was it helpful?

Solution

The current recommendation is to use python -m pip, where python is the version of Python you would like to use. This is the recommendation because it works across all versions of Python, and in all forms of virtualenv. For example:

# The system default python:
$ python -m pip install fish

# A virtualenv's python:
$ .env/bin/python -m pip install fish

# A specific version of python:
$ python-3.6 -m pip install fish

Previous answer, left for posterity:

Since version 0.8, Pip supports pip-{version}. You can use it the same as easy_install-{version}:

$ pip-2.5 install myfoopackage
$ pip-2.6 install otherpackage
$ pip-2.7 install mybarpackage

EDIT: pip changed its schema to use pipVERSION instead of pip-VERSION in version 1.5. You should use the following if you have pip >= 1.5:

$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage

Check https://github.com/pypa/pip/pull/1053 for more details


References:

OTHER TIPS

On Windows, you can execute the pip module using a given Python version through the Python launcher, py.exe, if you chose to install it during Python 3 setup.

py -3 -m pip install packagename
py -2 -m pip install packagename

You can be even more specific and request an exact sub-version of Python:

py -3.6 -m pip install packagename

To get a list of all installed Python versions available through the launcher, run:

py --list

Alternatively, you can launch the desired Python executable directly:

C:/path/to/specific/python.exe -m pip install packagename

/path/to/python2.{5,6} /path/to/pip install PackageName doesn't work?

For this to work on any python version that doesn't have pip already installed you need to download pip and do python*version* setup.py install. For example python3.3 setup.py install. This resolves the import error in the comments. (As suggested by @hbdgaf)

I had python 2.6 installed by default (Amazon EC2 AMI), but needed python2.7 plus some external packages for my application. Assuming you already installed python2.7 alongside with default python (2.6 in my case). Here is how to install pip and packages for non-default python2.7

Install pip for your python version:

curl -O https://bootstrap.pypa.io/get-pip.py
python27 get-pip.py

Use specific pip version to install packages:

pip2.7 install mysql-connector-python --allow-external mysql-connector-python

It worked for me in windows this way:

  1. I changed the name of python files python.py and pythonw.exe to python3.py pythonw3.py

  2. Then I just ran this command in the prompt:

    python3 -m pip install package

Other answers show how to use pip with both 2.X and 3.X Python, but does not show how to handle the case of multiple Python distributions (eg. original Python and Anaconda Python).

I have a total of 3 Python versions: original Python 2.7 and Python 3.5 and Anaconda Python 3.5.

Here is how I install a package into:

  1. Original Python 3.5:

    /usr/bin/python3 -m pip install python-daemon
    
  2. Original Python 2.7:

    /usr/bin/python -m pip install python-daemon
    
  3. Anaconda Python 3.5:

    python3 -m pip install python-daemon
    

    or

    pip3 install python-daemon
    

    Simpler, as Anaconda overrides original Python binaries in user environment.

    Of course, installing in anaconda should be done with conda command, this is just an example.


Also, make sure that pip is installed for that specific python.You might need to manually install pip. This works in Ubuntu 16.04:

sudo apt-get install python-pip 

or

sudo apt-get install python3-pip

I ran into this issue myself recently and found that I wasn't getting the right pip for Python 3, on my Linux system that also has Python 2.

First you must ensure that you have installed pip for your python version:

For Python 2:

sudo apt-get install python-pip

For Python 3:

sudo apt-get install python3-pip

Then to install packages for one version of Python or the other, simply use the following for Python 2:

pip install <package>

or for Python 3:

pip3 install <package>

pip is also a python package. So the easiest way to install modules to a specific python version would be below

 python2.7 /usr/bin/pip install foo

or

python2.7 -m pip install foo

So apparently there are multiple versions of easy_install and pip. It seems to be a big mess. Anyway, this is what I did to install Django for Python 2.7 on Ubuntu 12.10:

$ sudo easy_install-2.7 pip
Searching for pip
Best match: pip 1.1
Adding pip 1.1 to easy-install.pth file
Installing pip-2.7 script to /usr/local/bin

Using /usr/lib/python2.7/dist-packages
Processing dependencies for pip
Finished processing dependencies for pip

$ sudo pip-2.7 install django
Downloading/unpacking django
  Downloading Django-1.5.1.tar.gz (8.0Mb): 8.0Mb downloaded
  Running setup.py egg_info for package django

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: django
  Running setup.py install for django
    changing mode of build/scripts-2.7/django-admin.py from 644 to 755

    warning: no previously-included files matching '__pycache__' found under directory '*'
    warning: no previously-included files matching '*.py[co]' found under directory '*'
    changing mode of /usr/local/bin/django-admin.py to 755
Successfully installed django
Cleaning up...

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> 

From here: https://docs.python.org/3/installing/

Here is how to install packages for various versions that are installed at the same time linux, mac, posix:

python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage  # specifically Python 2.7
python3   -m pip install SomePackage  # default Python 3
python3.4 -m pip install SomePackage  # specifically Python 3.4
python3.5 -m pip install SomePackage  # specifically Python 3.5
python3.6 -m pip install SomePackage  # specifically Python 3.6

On Windows, use the py Python launcher in combination with the -m switch:

py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3   -m pip install SomePackage  # default Python 3
py -3.4 -m pip install SomePackage  # specifically Python 3.4

On Linux, Mac OS X and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:

python2.7 -m pip install SomePackage
python3.4 -m pip install SomePackage

(appropriately versioned pip commands may also be available)

On Windows, use the py Python launcher in combination with the -m switch:

py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3.4 -m pip install SomePackage  # specifically Python 3.4

if you get an error for py -3.4 then try:

pip install SomePackage

Most of the answers here address the issue but I want to add something what was continually confusing me with regard to creating an alternate installation of python in the /usr/local on CentOS 7. When I installed there, it appeared like pip was working since I could use pip2.7 install and it would install modules. However, what I couldn't figure out was why my newly installed version of python wasn't seeing what I was installing.

It turns out in CentOS 7 that there is already a python2.7 and a pip2.7 in the /usr/bin folder. To install pip for your new python distribution, you need to specifically tell sudo to go to /usr/local/bin

sudo /usr/local/bin/python2.7 -m ensurepip

This should get pip2.7 installed in your /usr/local/bin folder along with your version of python. The trick is that when you want to install modules, you either need to modify the sudo $PATH variable to include /usr/local/bin or you need to execute

sudo /usr/local/bin/pip2.7 install <module>

if you want to install a new module. It took me forever to remember that sudo wasn't immediately seeing /usr/local/bin.

Installation of multiple versions of Python and respective Packages.

Python version on the same windows machine : 2.7 , 3.4 and 3.6

Installation of all 3 versions of Python :

  • Installed the Python 2.7 , 3.4 and 3.6 with the below paths

enter image description here

PATH for all 3 versions of Python :

  • Made sure the PATH variable ( in System Variables ) has below paths included - C:\Python27\;C:\Python27\Scripts;C:\Python34\;C:\Python34\Scripts;C:\Python36\;C:\Python36\Scripts\;

Renaming the executables for versions :

  • Changed the python executable name in C:\Python36 and C:\Python34 to python36 and python34 respectively.

enter image description here

Checked for the command prompt with all versions :

enter image description here

Installing the packages separately for each version

enter image description here

Context: Archlinux

Action:
Install python2-pip:
sudo pacman -S python2-pip

You now have pip2.7:
sudo pip2.7 install boto

Test (in my case I needed 'boto'):
Run the following commands:

python2
import boto

Success: No error.

Exit: Ctrl+D

for example, if you set other versions (e.g. 3.5) as default and want to install pip for python 2.7:

  1. download pip at https://pypi.python.org/pypi/pip (tar)
  2. unzip tar file
  3. cd to the file’s directory
  4. sudo python2.7 setup.py install

You can go to for example C:\Python2.7\Scripts and then run cmd from that path. After that you can run pip2.7 install yourpackage...

That will install package for that version of Python.

Here is my take on the problem. Works for Python3. The main features are:

  • Each Python version is compiled from source
  • All versions are installed locally
  • Does not mangle your system's default Python installation in any way
  • Each Python version is isolated with virtualenv

The steps are as follows:

  1. If you have several extra python versions installed in some other way, get rid of them, e.g., remove $HOME/.local/lib/python3.x, etc. (also the globally installed ones). Don't touch your system's default python3 version though.

  2. Download source for different python versions under the following directory structure:

    $HOME/
        python_versions/ : download Python-*.tgz packages here and "tar xvf" them.  You'll get directories like this:
          Python-3.4.8/
          Python-3.6.5/
          Python-3.x.y/
          ...
    
  3. At each "Python-3.x.y/" directory, do the following (do NOT use "sudo" in any of the steps!):

    mkdir root
    ./configure --prefix=$PWD/root 
    make -j 2
    make install
    virtualenv --no-site-packages -p root/bin/python3.x env
    
  4. At "python_versions/" create files like this:

    env_python3x.bash:
    
    #!/bin/bash
    echo "type deactivate to exit"
    source $HOME/python_versions/Python-3.x.y/env/bin/activate
    
  5. Now, anytime you wish to opt for python3.x, do

    source $HOME/python_versions/env_python3x.bash
    

    to enter the virtualenv

  6. While in the virtualenv, install your favorite python packages with

    pip install --upgrade package_name
    
  7. To exit the virtualenv and python version just type "deactivate"

This is probably the completely wrong thing to do (I'm a python noob), but I just went in and edited the pip file

#!/usr/bin/env python3 <-- I changed this line.

# -*- coding: utf-8 -*-
import re
import sys

from pip._internal import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

For windows specifically: \path\to\python.exe -m pip install PackageName works.

If you have multiple versions as well as multiple architectures (32 bit, 64 bit) you will need to add a -32 or -64 at the end of your version.

For windows, go to cmd and type py --list and it will produce the versions you have installed. The list will look like the following:

Installed Pythons found by py Launcher for Windows
 -3.7-64 *
 -3.7-32
 -3.6-32

The full command as an example will be:

py -3.6-32 -m pip install (package)

If you want to get more indepth, to install a specific version of a package on a specific version of python, use ==(version) after the package. As an example,

py -3.6-32 -m pip install opencv-python==4.1.0.25
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top