Install a module using pip for specific python version

PythonPip

Python Problem Overview


On Ubuntu 10.04 by default Python 2.6 is installed, then I have installed Python 2.7. How can I use pip install to install packages for Python 2.7.

For example:

pip install beautifulsoup4

by default installs BeautifulSoup for Python 2.6

When I do:

import bs4

in Python 2.6 it works, but in Python 2.7 it says:

No module named bs4

Python Solutions


Solution 1 - Python

Alternatively, since pip itself is written in python, you can just call it with the python version you want to install the package for:

python2.7 -m pip install foo

Solution 2 - Python

Use a version of pip installed against the Python instance you want to install new packages to.

In many distributions, there may be separate python2.6-pip and python2.7-pip packages, invoked with binary names such as pip-2.6 and pip-2.7. If pip is not packaged in your distribution for the desired target, you might look for a setuptools or easyinstall package, or use virtualenv (which will always include pip in a generated environment).

pip's website includes installation instructions, if you can't find anything within your distribution.

Solution 3 - Python

You can execute pip module for a specific python version using the corresponding python:

Python 2.6:

python2.6 -m pip install beautifulsoup4

Python 2.7

python2.7 -m pip install beautifulsoup4

Solution 4 - Python

In Windows, you can execute the pip module by mentioning the python version ( You need to ensure that the launcher is on your path )

py -2 -m pip install pyfora

Solution 5 - Python

You can use this syntax

python_version -m pip install your_package

For example. If you're running python3.5, you named it as "python3", and want to install numpy package

python3 -m pip install numpy

Solution 6 - Python

Have tried this on a Windows machine and it works

If you wanna install opencv for python version 3.7, heres how you do it!

py -3.7 -m pip install opencv-python

Solution 7 - Python

Alternatively, if you want to install specific version of the package with the specific version of python, this is the way

sudo python2.7 -m pip install pyudev=0.16

if the "=" doesnt work, use ==

x@ubuntuserv:~$ sudo python2.7 -m pip install pyudev=0.16

Invalid requirement: 'pyudev=0.16' = is not a valid operator. Did you mean == ?

x@ubuntuserv:~$ sudo python2.7 -m pip install pyudev==0.16

works fine

Solution 8 - Python

If you have both 2.7 and 3.x versions of python installed, then just rename the python exe file of python 3.x version to something like - "python.exe" to "python3.exe". Now you can use pip for both versions individually. If you normally type "pip install " it will consider the 2.7 version by default. If you want to install it on the 3.x version you need to call the command as "python3 -m pip install ".

Solution 9 - Python

Python 2

sudo pip2 install johnbonjovi  
	

Python 3

sudo pip3 install johnbonjovi

Solution 10 - Python

For Python 3

sudo apt-get install python3-pip
sudo pip3 install beautifulsoup4

For Python 2

sudo apt-get install python2-pip
sudo pip2 install beautifulsoup4

> On Debian/Ubuntu, pip is the command to use when installing packages > for Python 2, while pip3 is the command to use when installing > packages for Python 3.

Solution 11 - Python

for python2 use:

py -2 -m pip install beautifulsoup4

Solution 12 - Python

I faced a similar problem with another package called Twisted. I wanted to install it for Python 2.7, but it only got installed for Python 2.6 (system's default version).

Making a simple change worked for me.

When adding Python 2.7's path to your $PATH variable, append it to the front like this: PATH=/usr/local/bin:$PATH, so that the system uses that version.

If you face more problems, you can follow this blog post which helped me - https://github.com/h2oai/h2o-2/wiki/installing-python-2.7-on-centos-6.3.-follow-this-sequence-exactly-for-centos-machine-only

Solution 13 - Python

As with any other python script, you may specify the python installation you'd like to run it with. You may put this in your shell profile to save the alias. The $1 refers to the first argument you pass to the script.

# PYTHON3 PIP INSTALL V2
alias pip_install3="python3 -m $(which pip) install $1"

Solution 14 - Python

I had Python 2.7 installed via chocolatey on Windows and found pip2.7.exe in C:\tools\python2\Scripts.

Using this executable instead of the pip command installed the correct module for me (requests for Python 2.7).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontorayeffView Question on Stackoverflow
Solution 1 - PythonTurionView Answer on Stackoverflow
Solution 2 - PythonCharles DuffyView Answer on Stackoverflow
Solution 3 - PythonTiago CoutinhoView Answer on Stackoverflow
Solution 4 - PythonmonView Answer on Stackoverflow
Solution 5 - PythonCatbuiltsView Answer on Stackoverflow
Solution 6 - PythonAnurag BhalekarView Answer on Stackoverflow
Solution 7 - Pythonravi.zombieView Answer on Stackoverflow
Solution 8 - PythonBhaskar BhuyanView Answer on Stackoverflow
Solution 9 - PythonDavid McCorkleView Answer on Stackoverflow
Solution 10 - PythonGayan WeerakuttiView Answer on Stackoverflow
Solution 11 - Pythonamalik2205View Answer on Stackoverflow
Solution 12 - PythonRajiView Answer on Stackoverflow
Solution 13 - PythonRKelleyView Answer on Stackoverflow
Solution 14 - PythonCodeManXView Answer on Stackoverflow