问题:pip或pip3为Python 3安装软件包?

我有一台配备OS X El Captain的Macbook。我认为那Python 2.7是预装的。但是,我也安装Python 3.5了。开始使用时Python 3,我读到要安装软件包,应输入:

pip3 install some_package

无论如何,现在当我使用

pip install some_package

some_package安装了Python 3。我的意思是我可以导入它,并且可以毫无问题地使用它。而且,当我只pip3输入时Terminal,我得到以下关于用法的消息:

Usage:   
  pip <command> [options]

这是我输入just时得到的相同信息pip

这是否意味着在previos版本,事情是不同的,现在pippip3可以互换使用?如果是这样,并且为了参数起见,我该如何安装软件包Python 2而不是Python 3

I have a Macbook with OS X El Captain. I think that Python 2.7 comes preinstalled on it. However, I installed Python 3.5 too. When I started using Python 3, I read that if I want to install a package, I should type:

pip3 install some_package

Anyway, now when I use

pip install some_package

I get some_package installed for Python 3. I mean I can import it and use it without problems. Moreover, when I type just pip3 in Terminal, I got this message about the usage:

Usage:   
  pip <command> [options]

which is the same message I get when I type just pip.

Does it mean that in previos versions, things were different, and now pip and pip3 can be used interchangeably? If so, and for the sake of argument, how can I install packages for Python 2 instead of Python 3?


回答 0

pip是使用链接到同一可执行文件路径的软链接pip3。您可以使用以下命令来检查您的真实路径pippip3实际路径:

$ ls -l `which pip`
$ ls -l `which pip3`

您也可以使用以下命令了解更多详细信息:

$ pip show pip
$ pip3 show pip

当我们安装不同版本的python时,我们可能会创建以下软链接到

  • 将默认点设置为某些版本。
  • 为不同的版本创建不同的链接。

这是同样的情况pythonpython2python3

如果您对在不同情况下如何发生感兴趣,可以在下面获取更多信息:

Your pip is a soft link to the same executable file path with pip3. you can use the commands below to check where your pip and pip3 real paths are:

$ ls -l `which pip`
$ ls -l `which pip3`

You may also use the commands below to know more details:

$ pip show pip
$ pip3 show pip

When we install different versions of python, we may create such soft links to

  • set default pip to some version.
  • make different links for different versions.

It is the same situation with python, python2, python3

More information below if you’re interested in how it happens in different cases:


回答 1

如果您安装了python 2.x,然后安装了python3,则您的pip将指向pip3。您可以通过键入来验证pip --version与相同pip3 --version

在您的系统上,现在有了pip,pip2和pip3

如果需要,可以将pip更改为指向pip2而不是pip3。

If you had python 2.x and then installed python3, your pip will be pointing to pip3. you can verify that by typing pip --version which would be the same as pip3 --version.

On your system you have now pip, pip2 and pip3.

If you want you can change pip to point to pip2 instead of pip3.


回答 2

安装后python3,即pip3会安装。如果你没有其他的Python安装(如python2.7),然后链接创建这点pippip3

所以,如果有Python安装的任何其他版本(比python3等)。 pip通常指向首次安装。

When you install python3, pip3 gets installed. And if you don’t have another python installation(like python2.7) then a link is created which points pip to pip3.

So if there is no other version of python installed(other than python3). pip generally points to the first installation.


回答 3

这是一个棘手的问题。最后,如果您调用pip它,则将调用pip2pip3,具体取决于您如何设置系统。

This is a tricky subject. In the end, if you invoke pip it will invoke either pip2 or pip3, depending on how you set your system up.


回答 4

我认为pippip2并且pip3不是指向同一可执行文件路径的软链接。请注意以下命令并在我的linux终端中显示结果:

mrz@mrz-pc ~ $ ls -l `which pip`
-rwxr-xr-x 1 root root 292 Nov 10  2016 /usr/bin/pip
mrz@mrz-pc ~ $ ls -l `which pip2`
-rwxr-xr-x 1 root root 283 Nov 10  2016 /usr/bin/pip2
mrz@mrz-pc ~ $ ls -l `which pip3`
-rwxr-xr-x 1 root root 293 Nov 10  2016 /usr/bin/pip3
mrz@mrz-pc ~ $ pip -V
pip 9.0.1 from /home/mrz/.local/lib/python2.7/site-packages (python 2.7)
mrz@mrz-pc ~ $ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
mrz@mrz-pc ~ $ pip3 -V
pip 9.0.1 from /home/mrz/.local/lib/python3.5/site-packages (python 3.5)

如您所见,它们存在于不同的路径中。

pip3始终仅在Python3环境上运行,就像pip2在Python2上一样。pip在适合上下文的任何环境下运行。例如,如果您在Python3平台上,pip将在Python3环境下运行。

I think pip, pip2 and pip3 are not soft links to the same executable file path. Note these commands and results in my Linux terminal:

mrz@mrz-pc ~ $ ls -l `which pip`
-rwxr-xr-x 1 root root 292 Nov 10  2016 /usr/bin/pip
mrz@mrz-pc ~ $ ls -l `which pip2`
-rwxr-xr-x 1 root root 283 Nov 10  2016 /usr/bin/pip2
mrz@mrz-pc ~ $ ls -l `which pip3`
-rwxr-xr-x 1 root root 293 Nov 10  2016 /usr/bin/pip3
mrz@mrz-pc ~ $ pip -V
pip 9.0.1 from /home/mrz/.local/lib/python2.7/site-packages (python 2.7)
mrz@mrz-pc ~ $ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
mrz@mrz-pc ~ $ pip3 -V
pip 9.0.1 from /home/mrz/.local/lib/python3.5/site-packages (python 3.5)

As you see they exist in different paths.

pip3 always operates on the Python3 environment only, as pip2 does with Python2. pip operates in whichever environment is appropriate to the context. For example, if you are in a Python3 venv, pip will operate on the Python3 environment.


回答 5

通过插图:

pip --version
  pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)

pip3 --version
  pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)

python --version
  Python 3.7.3

which python
  /usr/bin/python

ls -l '/usr/bin/python'
  lrwxrwxrwx 1 root root 7 Mar 26 14:43 /usr/bin/python -> python3

which python3
  /usr/bin/python3

ls -l /usr/bin/python3
  lrwxrwxrwx 1 root root 9 Mar 26 14:43 /usr/bin/python3 -> python3.7

ls -l /usr/bin/python3.7
  -rwxr-xr-x 2 root root 14120 Mar 26 14:43 /usr/bin/python3.7

因此,在我的默认系统python(Python 3.7.3)中,my pip pip3

By illustration:

pip --version
  pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)

pip3 --version
  pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)

python --version
  Python 3.7.3

which python
  /usr/bin/python

ls -l '/usr/bin/python'
  lrwxrwxrwx 1 root root 7 Mar 26 14:43 /usr/bin/python -> python3

which python3
  /usr/bin/python3

ls -l /usr/bin/python3
  lrwxrwxrwx 1 root root 9 Mar 26 14:43 /usr/bin/python3 -> python3.7

ls -l /usr/bin/python3.7
  -rwxr-xr-x 2 root root 14120 Mar 26 14:43 /usr/bin/python3.7

Thus, my in my default system python (Python 3.7.3), pip is pip3.


回答 6

如果您安装了Python 2.7,我想您可以使用pip2pip2.7安装专门针对Python 2的软件包,例如

pip2 install some_pacakge

要么

pip2.7 install some_package

您可以使用pip3pip3.5安装专门用于Python 3的pacakges。

If you installed Python 2.7, I think you could use pip2 and pip2.7 to install packages specifically for Python 2, like

pip2 install some_pacakge

or

pip2.7 install some_package

And you may use pip3 or pip3.5 to install pacakges specifically for Python 3.


回答 7

在我的Windows实例上-并且我不完全了解我的环境-使用pip3安装kaggle-cli程序包有效-而pip无效。我在conda环境中工作,环境似乎有所不同。

(fastai)C:\ Users \ redact \ Downloads \ fast.ai \ deeplearning1 \ nbs> pip –version

来自C:\ ProgramData \ Anaconda3 \ envs \ fastai \ lib \ site-packages的pip 9.0.1(python 3.6)

(fastai)C:\ Users \ redact \ Downloads \ fast.ai \ deeplearning1 \ nbs> pip3 –version

来自c:\ users \ redact \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages(python 3.6)的pip 9.0.1

On my Windows instance – and I do not fully understand my environment – using pip3 to install the kaggle-cli package worked – whereas pip did not. I was working in a conda environment and the environments appear to be different.

(fastai) C:\Users\redact\Downloads\fast.ai\deeplearning1\nbs>pip –version

pip 9.0.1 from C:\ProgramData\Anaconda3\envs\fastai\lib\site-packages (python 3.6)

(fastai) C:\Users\redact\Downloads\fast.ai\deeplearning1\nbs>pip3 –version

pip 9.0.1 from c:\users\redact\appdata\local\programs\python\python36\lib\site-packages (python 3.6)


回答 8

somepath / venv中激活了Python 3.6 virtualenv后,以下别名解决了macOS Sierra上的各种问题,其中pip坚持指向Apple的2.7 Python。

alias pip='python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'

当我不得不做的时候,这不是很好,sudo pip因为root用户对我的别名或virtualenv一无所知,所以我也不得不添加一个额外的别名来处理这个问题。这是一个hack,但是可以用,我知道它的作用:

alias sudopip='sudo somepath/venv/bin/python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'

背景:

pip3不存在,无法启动(找不到命令),which pip它将返回Apple Python的/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/pip

Python 3.6是通过macports安装的。

激活我想使用的3.6 virtualenv后,which python将返回somepath / venv / bin / python

以某种方式pip install可以做正确的事情并击中我的virtualenv,但pip list会使Python 2.7包感到不安。

对于Python而言,这对初学者友好程度比我期望的要低。

Given an activated Python 3.6 virtualenv in somepath/venv, the following aliases resolved the various issues on a macOS Sierra where pip insisted on pointing to Apple’s 2.7 Python.

alias pip='python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'

This didn’t work so well when I had to do sudo pip as the root user doesn’t know anything about my alias or the virtualenv, so I had to add an extra alias to handle this as well. It’s a hack, but it works, and I know what it does:

alias sudopip='sudo somepath/venv/bin/python somepath/venv/lib/python3.6/site-packages/pip/__main__.py'

background:

pip3 did not exist to start (command not found) with and which pip would return /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/pip, the Apple Python.

Python 3.6 was installed via macports.

After activation of the 3.6 virtualenv I wanted to work with, which python would return somepath/venv/bin/python

Somehow pip install would do the right thing and hit my virtualenv, but pip list would rattle off Python 2.7 packages.

For Python, this is batting way beneath my expectations in terms of beginner-friendliness.


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