Install a module using pip for specific python ver

2019-01-02 17:26发布

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 pip
12条回答
忆尘夕之涩
2楼-- · 2019-01-02 17:34

Python 2

sudo pip2 install johnbonjovi  

Python 3

sudo pip3 install johnbonjovi
查看更多
大哥的爱人
3楼-- · 2019-01-02 17:34

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"
查看更多
余欢
4楼-- · 2019-01-02 17:37

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 ".

查看更多
浅入江南
5楼-- · 2019-01-02 17:37

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).

查看更多
琉璃瓶的回忆
6楼-- · 2019-01-02 17:39

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 /usr/bin/pip install foo

Edit: Or, as per llopis' remark:

python2.7 -m pip install foo
查看更多
听够珍惜
7楼-- · 2019-01-02 17:41

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
查看更多
登录 后发表回答