Python 在 Amazon Linux 中升级 pip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34103119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:28:11  来源:igfitidea点击:

Upgrade pip in Amazon Linux

pythonamazon-ec2pip

提问by zeck

I wanted to deploy my Python app on Amazon Linux AMI 2015.09.1, which has Python2.7 (default) and pip (6.1.1). Then, I upgraded the pip using the command:

我想在 Amazon Linux AMI 2015.09.1 上部署我的 Python 应用程序,它具有 Python2.7(默认)和 pip(6.1.1)。然后,我使用以下命令升级了 pip:

sudo pip install -U pip

However, it seemed broken, and showed the message when I tried to install packages:

但是,它似乎坏了,并在我尝试安装软件包时显示了消息:

pkg_resources.DistributionNotFound: pip==6.1.1

I found out that pip remove the previous files located in /usr/bin/, and installed the new one in /usr/local/bin. Thus, I tried to specify the location by using the command:

我发现,PIP去除位于以前的文件/usr/bin/,并安装在新的一个/usr/local/bin。因此,我尝试使用以下命令指定位置:

sudo pip install -U --install-option="--prefix='/usr/bin'" pip

Nevertheless, it still installed the new one in /usr/local/bin. In addition to that, pip could not work well with sudoalthough it successfully installed. The error message :

尽管如此,它仍然在/usr/local/bin. 除此之外,sudo尽管pip安装成功,但它无法正常工作。错误信息:

sudo: pip2.7: command not found

Is there a way to properly manage pip?

有没有办法正确管理pip?

回答by Jihoon Baek

Try:

尝试:

sudo which pip

This may reveal something like 'no pip in ($PATH)'.

这可能会揭示诸如“($PATH) 中没有 pip”之类的信息。

If that is the case, you can then do:

如果是这种情况,您可以执行以下操作:

which pip

Which will give you a path like /usr/local/bin/pip.

这会给你一个像/usr/local/bin/pip.

Copy + paste the path to pip to the sbin folder by running: sudo cp /usr/local/bin/pip /usr/sbin/

通过运行将 pip 的路径复制并粘贴到 sbin 文件夹: sudo cp /usr/local/bin/pip /usr/sbin/

This will allow you to run sudo pipwithout any errors.

这将允许您运行sudo pip而不会出现任何错误。

回答by Scott Nguyen

I think you've didn't installed the pythonXX-pippackage.

我想你没有安装pythonXX-pip包。

I've upgraded mine straight to Python3.4, these commands works for me.

我已经将我的直接升级到 Python3.4,这些命令对我有用。

sudo su
yum install python34
yum install python34-pip

回答by angelokh

This works for me

这对我有用

sudo /usr/local/bin/pip install --upgrade pip

回答by William Holroyd

The problem is partly answered by your question. The Amazon AMI doesn't consider /usr/local/binto be part of the root account's PATH. You can fix this by updating the root account's ~/.bashrcto include it.

你的问题部分回答了这个问题。Amazon AMI 不认为/usr/local/bin是根账户的 PATH 的一部分。您可以通过更新 root 帐户~/.bashrc以包含它来解决此问题。

Something like this...

像这样的东西...

export PATH=$PATH:/usr/local/bin

export PATH=$PATH:/usr/local/bin

回答by Sector95

Struggled with this for a while. Here's what I've found:

为此苦苦挣扎了一段时间。这是我发现的:

  • ec2_userfinds the pipexecutable, but has a module import error due to otherhaving no read/execute permissions on the pipfolders in the /usr/local/lib/python2.7/site-packagesfolder. This is actually okay, since in most cases, pipinstalls fail when not run as rootanyway.
  • sudocannot find pip.
  • Entering rootwith sudo su -allows pipto be run without issue.
  • ec2_user找到pip可执行文件,但由于对文件夹中的文件夹other没有读取/执行权限而出现模块导入错误。这实际上是可以的,因为在大多数情况下,安装会在不按任何方式运行时失败。pip/usr/local/lib/python2.7/site-packagespiproot
  • sudo找不到pip
  • 进入rootsudo su -允许pip无问题运行。

The reason sudo pipstops working after the upgrade, is because the executable (or symbolic link) is removed from /usr/bin. However, what remains is a file called pip-27, which contains the following:

sudo pip升级后停止工作的原因是因为可执行文件(或符号链接)从/usr/bin. 但是,剩下的是一个名为 的文件pip-27,其中包含以下内容:

#!/usr/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==6.1.1','console_scripts','pip2.7'
__requires__ = 'pip==6.1.1'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.exit(
        load_entry_point('pip==6.1.1', 'console_scripts', 'pip2.7')()
    )

So, that's where our error comes from, as the upgrade clearly doesn't clean this file up. Not entirely clear on where the name translation from pipto pip-2.7occurs.

所以,这就是我们的错误的来源,因为升级显然没有清理这个文件。不完全清楚从piptopip-2.7发生的名称翻译。

As mentioned in another answer, pipnow exists in /usr/local/binafter the upgrade, which is no longer in the sudosecure path. You can add this path to the secure_pathvariable by running sudo visudo. Another alternative, if you'd prefer to not add that path to your secure_pathis to make a symbolic link to the new pipexecutable in /usr/bin.

正如另一个答案中提到的,pip现在存在于/usr/local/bin升级后,不再处于sudo安全路径中。您可以secure_path通过运行将此路径添加到变量中sudo visudo。另一种选择,如果您不想将该路径添加到您的路径中,secure_pathpip/usr/bin.

回答by Tosin

After struggling with this for hours and reading comments

在为此挣扎了几个小时并阅读了评论之后

which pipgave /usr/bin/pip , but the actual pip was located at /usr/local/bin/pip due to pip upgrade and clean up was not complete

which pip给出了 /usr/bin/pip ,但由于 pip 升级和清理未完成,实际 pip 位于 /usr/local/bin/pip

So removing the pip in /usr/bin/

所以删除 /usr/bin/ 中的 pip

sudo rm /usr/bin/pip

须藤rm /usr/bin/pip

and also adding the new pip to your export path

并将新的 pip 添加到您的导出路径

vim ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/bin

vim ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/bin

exit terminal, and login back

退出终端,然后重新登录

which pipshould give /usr/local/bin/pip

which pip应该给 /usr/local/bin/pip

pip install --upgrade pip

回答by Hakan B.

I think the best strategy in this case is to manage pipis as part of a virtual environment using virtualenvrather than messing with the system-level version.

我认为在这种情况下最好的策略pip是作为虚拟环境的一部分进行管理,virtualenv而不是使用系统级版本。

If you're OK with that, here's the basic idea:

如果你同意,这里是基本的想法:

  1. Install the version of virtualenvpackaged with the version of pipyou are looking to upgrade to to the system-level pip(e.g. virtualenv==15.1.0comes with pip==9.0.1):

    $ sudo pip install -U virtualenv==15.1.0
    You are using pip version 6.1.1, however version 9.0.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
    Collecting virtualenv==15.1.0
      Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
        100% |████████████████████████████████| 1.8MB 135kB/s
    Installing collected packages: virtualenv
      Found existing installation: virtualenv 12.0.7
        Uninstalling virtualenv-12.0.7:
          Successfully uninstalled virtualenv-12.0.7
    Successfully installed virtualenv-15.1.0
    

    I used the virtualenvrelease notesto find out which version of pipcorresponds to which version of virtualenv.

  2. Create the virtual environment:

    $ virtualenv myenv
    New python executable in /home/ec2-user/myenv/bin/python2.7
    Also creating executable in /home/ec2-user/myenv/bin/python
    Installing setuptools, pip, wheel...done.
    
  3. Activate the virtual environment and confirm the version and location of the upgraded pip:

    $ source myenv/bin/activate
    (myenv) $ pip -V
    pip 9.0.1 from /home/ec2-user/myenv/local/lib/python2.7/dist-packages (python 2.7)
    (myenv) $ which pip
    ~/myenv/bin/pip
    
  1. 安装virtualenvpip您要升级到系统级的版本一起打包的版本pip(例如virtualenv==15.1.0附带pip==9.0.1):

    $ sudo pip install -U virtualenv==15.1.0
    You are using pip version 6.1.1, however version 9.0.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
    Collecting virtualenv==15.1.0
      Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
        100% |████████████████████████████████| 1.8MB 135kB/s
    Installing collected packages: virtualenv
      Found existing installation: virtualenv 12.0.7
        Uninstalling virtualenv-12.0.7:
          Successfully uninstalled virtualenv-12.0.7
    Successfully installed virtualenv-15.1.0
    

    我使用virtualenv发行说明来找出哪个版本pip对应于哪个版本的virtualenv.

  2. 创建虚拟环境:

    $ virtualenv myenv
    New python executable in /home/ec2-user/myenv/bin/python2.7
    Also creating executable in /home/ec2-user/myenv/bin/python
    Installing setuptools, pip, wheel...done.
    
  3. 激活虚拟环境并确认升级后的版本和位置pip

    $ source myenv/bin/activate
    (myenv) $ pip -V
    pip 9.0.1 from /home/ec2-user/myenv/local/lib/python2.7/dist-packages (python 2.7)
    (myenv) $ which pip
    ~/myenv/bin/pip
    

This should allow you to install packages to this virtualenvusing the pipversion of your choice, without the need for sudo.

这应该允许您virtualenv使用pip您选择的版本安装软件包,而无需sudo.

回答by granpappypimp

To add to angelokh

添加到angelokh

sudo `which pip` install --upgrade pip