Pip突然使用错误版本的Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36311336/
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 17:43:34  来源:igfitidea点击:

Pip suddenly using wrong version of Python

pythonmacospip

提问by jcmiller11

Having a weird problem with pip on os x.

在 os x 上使用 pip 有一个奇怪的问题。

As far as I can recall (and a quick look at my .bash_history seems to confirm) I have not made any recent changes to my configuration. Alas, the pip command seems to be suddenly using a different version of python than it was previously. Up until now I was using the command pip to manage my python2 libraries and pip3 to manage by python3 libraries. Suddenly, any attempts at running pip install fails with errors like missing parenthesis around print statements.

据我所知(快速查看我的 .bash_history 似乎可以确认)我最近没有对我的配置进行任何更改。唉,pip 命令似乎突然使用了与以前不同版本的 python。到目前为止,我使用命令 pip 来管理我的 python2 库和 pip3 由 python3 库管理。突然,任何运行 pip install 的尝试都会失败,并出现诸如缺少打印语句周围的括号之类的错误。

Here is the result of a few commands I attempted to figure out the problem:

这是我试图找出问题的几个命令的结果:

which pip > /usr/local/bin/pip

which pip3 > /usr/local/bin/pip3

which python > /usr/local/bin/python

python version > Python 2.7.11

pip --version > pip 8.1.1 from /usr/local/lib/python3.5/site-packages (python 3.5)

So for some reason the pip command seems to be running from the PyPi2 database but in python3 now? Any ideas how to fix this?

因此,出于某种原因,pip 命令似乎是从 PyPi2 数据库运行的,但现在在 python3 中运行?任何想法如何解决这一问题?

回答by

I run with multiple Python versions and thus multiple pip versions as well.

我运行多个 Python 版本,因此也运行多个 pip 版本。

Everytime, however, you update pip, you'll replace the standard pipcommand with the version you updated. So even pip3 install --upgrade pipwill put a /usr/local/bin/pipin your system, messing up the Python 2 version.

但是,每次更新 pip 时,都会用pip更新的版本替换标准命令。所以甚至pip3 install --upgrade pip/usr/local/bin/pip在你的系统中放一个,搞乱 Python 2 版本。

Instead, I run pip as an (executable) module:

相反,我将 pip 作为(可执行)模块运行:

python3 -m pip search <package>

or

或者

python2 -m pip search <package>

or even

甚至

python3.5 -m pip search <package>

This guarantees that your pipversion always matches the Python version you want to use it for. It's somewhat longer to type, but I prefer the expliciteness of it (which, I guess, follows the Zen of Python).

这可以保证您的pip版本始终与您要使用它的 Python 版本相匹配。打字时间有点长,但我更喜欢它的明确性(我猜,这遵循 Python 的 Zen)。

Note that updating pip:

请注意,更新 pip:

python3.5 -m pip install --upgrade pip

will still install a Python 3.5 version in /usr/local/bin/pip, but I'm simply ignoring that. Just beware of (shell) scripts that execute pipdirectly.

仍将在 中安装 Python 3.5 版本/usr/local/bin/pip,但我只是忽略了这一点。请注意pip直接执行的(shell)脚本。

回答by shihs

I had exactly the same problem!

我遇到了完全相同的问题!

I reinstall python2 by brew brew reinstall python@2

我通过 brew 重新安装 python2 brew reinstall python@2

after reinstall, pip install packagenameworks!

重新安装后,pip install packagename工作!

回答by alec_djinn

Try to set aliases by running the following commands in Terminal,

尝试通过在终端中运行以下命令来设置别名,

alias pip="/usr/local/bin/pip"
alias pip2="/usr/local/bin/pip"
alias pip3="/usr/local/bin/pip3"

If this solves your problem then you need to add the aliases in your bash profile. Look How do I create a Bash alias?for more info.

如果这解决了您的问题,那么您需要在 bash 配置文件中添加别名。看看如何创建 Bash 别名?了解更多信息。

Alternatively, you have to reinstall pip using python2 get-pip.pyfirst and then python3 get-pip.pyget-pip.py can be downloaded here https://bootstrap.pypa.io/get-pip.py

或者,您必须python2 get-pip.py首先使用 pip 重新安装,然后python3 get-pip.py可以在此处下载 get-pip.py https://bootstrap.pypa.io/get-pip.py

回答by Dmitry Tokarev

Find absolute path to Python you'd like to use:

找到你想使用的 Python 的绝对路径:

which python

Open your default pip executable script:

打开默认的 pip 可执行脚本:

vi $(which pip)

You will see a shebang line at the top which may point to wrong Python (i had that once too).

你会在顶部看到一条 shebang 线,它可能指向错误的 Python(我也有过一次)。

Point to the Python you want (see step 1), e.g.:

指向您想要的 Python(参见步骤 1),例如:

#!/usr/local/bin/python3.7