使用pip为特定的python版本安装模块

问题:使用pip为特定的python版本安装模块

在Ubuntu 10.04上,默认情况下安装了Python 2.6,然后我安装了Python 2.7。如何使用pip installPython 2.7安装软件包。

例如:

pip install beautifulsoup4

默认情况下会为Python 2.6安装BeautifulSoup

当我做:

import bs4

在Python 2.6中可以使用,但在Python 2.7中可以显示:

No module named bs4

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

回答 0

pip对要安装新软件包的Python实例使用已安装的版本。

在许多发行版中,可能会有单独的python2.6-pippython2.7-pip程序包,并使用诸如pip-2.6和的二进制名称来调用pip-2.7。如果未将pip打包到所需目标的发行版中,则可能需要寻找setuptools或easyinstall软件包,或使用virtualenv(在生成的环境中始终包含pip)。

如果您在发行版中找不到任何内容,请在pip的网站上提供安装说明

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.


回答 1

另外,由于pip其本身是用python编写的,因此您可以使用要为其安装软件包的python版本进行调用:

python2.7 -m pip install foo

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

回答 2

您可以使用相应的python为特定的python版本执行 pip模块:

Python 2.6:

python2.6 -m pip install beautifulsoup4

Python 2.7

python2.7 -m pip install beautifulsoup4

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

回答 3

您可以使用此语法

python_version -m pip install your_package

例如。如果您正在运行python3.5,则将其命名为“ python3”,并想安装numpy软件包

python3 -m pip install numpy

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

回答 4

在Windows中,您可以通过提及python版本来执行pip模块(您需要确保启动器在您的路径上)

py -2 -m pip install pyfora

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

回答 5

另外,如果您想使用特定版本的python安装软件包的特定版本,可以采用这种方法

sudo python2.7 -m pip install pyudev=0.16

如果“ =”无效,请使用==

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

无效的要求:’pyudev = 0.16’=不是有效的运算符。你是说==吗?

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

工作良好

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


回答 6

Python 2

sudo pip2 install johnbonjovi  

Python 3

sudo pip3 install johnbonjovi

Python 2

sudo pip2 install johnbonjovi  

Python 3

sudo pip3 install johnbonjovi

回答 7

如果您同时安装了2.7和3.x版本的python,则只需将python 3.x版本的python exe文件重命名为类似的名称-将“ python.exe”重命名为“ python3.exe”。现在,您可以将pip分别用于两个版本。如果您通常键入“ pip install”,则默认情况下将考虑2.7版本。如果要在3.x版本上安装它,则需要将命令调用为“ python3 -m pip install”。

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


回答 8

对于Python 3

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

对于Python 2

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

在Debian / Ubuntu上, pip是在安装适用于Python 2的软件包时使用的命令,pip3而是在安装适用于Python 3的软件包时使用的命令。

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.


回答 9

对于python2使用:

py -2 -m pip install beautifulsoup4

for python2 use:

py -2 -m pip install beautifulsoup4

回答 10

与其他任何python脚本一样,您可以指定运行它的python安装。您可以将其放在外壳配置文件中以保存别名。该$1指的是你传递给脚本的第一个参数。

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

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"

回答 11

我在Windows上通过Chocolatey安装了Python 2.7 ,并在pip2.7.exe中找到了C:\tools\python2\Scripts

使用此可执行文件而不是pip命令为我安装了正确的模块(requests对于Python 2.7)。

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


回答 12

我在另一个名为Twisted的软件包中也遇到了类似的问题。我想为Python 2.7安装它,但仅为Python 2.6(系统的默认版本)安装了它。

进行简单的更改对我有用。

在将Python 2.7的路径添加到$PATH变量时,请像这样将其追加到前面:PATH=/usr/local/bin:$PATH,以便系统使用该版本。

如果您遇到更多问题,可以关注这篇对我有帮助的博客文章-https: //github.com/h2oai/h2o-2/wiki/installing-python-2.7-on-centos-6.3.-follow-this-sequence仅用于centos机器

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