问题:如何在Python 3.x和Python 2.x上使用pip

我安装了Python 3.x(在Ubuntu上除了Python 2.x之外),然后慢慢开始配对在Python 2.x中使用的模块。

所以我想知道,应该为Python 2.x和Python 3.x使用pip来简化生活吗?

I installed Python 3.x (besides Python 2.x on Ubuntu) and slowly started to pair modules I use in Python 2.x.

So I wonder, what approach should I take to make my life easy by using pip for both Python 2.x and Python 3.x?


回答 0

您应该采取的方法是安装 pip为Python 3.2。

您可以通过以下方式执行此操作:

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

然后,您可以使用来安装适用于Python 3.2的内容pip-3.2,并使用来安装适用于Python 2-7的内容pip-2.7。该pip命令最终将指向其中之一,但是我不确定是哪一个,因此您必须进行检查。

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.


回答 1

您还可以做的是使用apt-get:

apt-get install python3-pip

以我的经验,这也很流利,而且您会从apt-get中获得所有好处。

What you can also do is to use apt-get:

apt-get install python3-pip

In my experience this works pretty fluent too, plus you get all the benefits from apt-get.


回答 2

首先,使用以下命令安装Python 3 pip:

sudo apt-get install python3-pip

然后,Python 3 pip使用:

pip3 install <module-name>

对于Python 2 pip使用:

pip install <module-name>

First, install Python 3 pip using:

sudo apt-get install python3-pip

Then, to use Python 3 pip use:

pip3 install <module-name>

For Python 2 pip use:

pip install <module-name>

回答 3

如果您不想每次使用pip时都指定版本:

安装点子:

$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python3

并导出路径:

$ export PATH=/Library/Frameworks/Python.framework/Versions/<version number>/bin:$PATH

If you don’t want to have to specify the version every time you use pip:

Install pip:

$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python3

and export the path:

$ export PATH=/Library/Frameworks/Python.framework/Versions/<version number>/bin:$PATH

回答 4

最短的方法:

python3 -m pip install package
python -m pip install package

The shortest way:

python3 -m pip install package
python -m pip install package

回答 5

这在OS X上对我有用:(我之所以这样说,是因为有时Mac对于每个开放源代码工具都拥有“自己的”版本,这是一种痛苦,并且您无法删除它,因为“其改进”使它对于其他苹果产品来说都是独一无二的,如果您将其删除,事情就会开始恶化)

我按照@Lennart Regebro提供的步骤来获取python 3的pip,但是python 2的pip仍然是第一个出现的路径,所以…我要做的是在/ usr / bin内创建指向python 3的符号链接(实际上,我也做了同样的事情来让我的2条python和平运行):

ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/pip /usr/bin/pip3

请注意,我3在末尾添加了一个,因此基本上您要做的是使用pip3而不是pip

该帖子很旧,但我希望有一天能对某人有所帮助。从理论上讲,这应该适用于任何LINUX系统。

This worked for me on OS X: (I say this because sometimes is a pain that mac has “its own” version of every open source tool, and you cannot remove it because “its improvements” make it unique for other apple stuff to work, and if you remove it things start falling appart)

I followed the steps provided by @Lennart Regebro to get pip for python 3, nevertheless pip for python 2 was still first on the path, so… what I did is to create a symbolic link to python 3 inside /usr/bin (in deed I did the same to have my 2 pythons running in peace):

ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/pip /usr/bin/pip3

Notice that I added a 3 at the end, so basically what you have to do is to use pip3 instead of just pip.

The post is old but I hope this helps someone someday. this should theoretically work for any LINUX system.


回答 6

在Suse Linux 13.2上,pip调用python3,但是pip2可用于使用旧版本的python。

On Suse Linux 13.2, pip calls python3, but pip2 is available to use the older python version.


回答 7

在Windows中,先安装Python 3.7,然后再安装Python 2.7。然后,使用命令提示符:

pip安装python2-module-name

pip3安装python3-module-name

就这样

In Windows, first installed Python 3.7 and then Python 2.7. Then, use command prompt:

pip install python2-module-name

pip3 install python3-module-name

That’s all


回答 8

请注意,在msys2上,我发现以下命令会有所帮助:

$ pacman -S python3-pip
$ pip3 install --upgrade pip
$ pip3 install --user package_name

Please note that on msys2 I’ve found these commands to be helpful:

$ pacman -S python3-pip
$ pip3 install --upgrade pip
$ pip3 install --user package_name

回答 9

以为这是个老问题,我想我有更好的解决方案

  1. 要将pip用于python 2.x环境,请使用以下命令-

    py -2 -m pip安装-r requirements.txt

  2. 要将pip用于python 3.x环境,请使用以下命令-

    py -3 -m pip install -r requirements.txt

Thought this is old question, I think I have better solution

  1. To use pip for a python 2.x environment, use this command –

    py -2 -m pip install -r requirements.txt

  2. To use pip for python 3.x environment, use this command –

    py -3 -m pip install -r requirements.txt


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。