简体   繁体   English

Pip突然使用错误版本的Python

[英]Pip suddenly using wrong version of Python

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.据我所知(快速查看我的 .bash_history 似乎可以确认)我最近没有对我的配置进行任何更改。 Alas, the pip command seems to be suddenly using a different version of python than it was previously.唉,pip 命令似乎突然使用了与以前不同版本的 python。 Up until now I was using the command pip to manage my python2 libraries and pip3 to manage by python3 libraries.到目前为止,我使用命令 pip 来管理我的 python2 库和 pip3 由 python3 库管理。 Suddenly, any attempts at running pip install fails with errors like missing parenthesis around print statements.突然,任何运行 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?因此,出于某种原因,pip 命令似乎是从 PyPi2 数据库运行的,但现在在 python3 中运行? Any ideas how to fix this?任何想法如何解决这一问题?

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 pip command with the version you updated.但是,每次更新 pip 时,您都会将标准pip命令替换为您更新的版本。 So even pip3 install --upgrade pip will put a /usr/local/bin/pip in your system, messing up the Python 2 version.所以即使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 pip version always matches the Python version you want to use it for.这可以保证您的pip版本始终与您要使用它的 Python 版本相匹配。 It's somewhat longer to type, but I prefer the expliciteness of it (which, I guess, follows the Zen of 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.仍将在/usr/local/bin/pip安装 Python 3.5 版本,但我只是忽略了这一点。 Just beware of (shell) scripts that execute pip directly.请注意直接执行pip的(shell)脚本。

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), eg:指向您想要的 Python(参见步骤 1),例如:

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

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.如果这解决了您的问题,那么您需要在 bash 配置文件中添加别名。 Look How do I create a Bash alias?看看如何创建 Bash 别名? for more info.了解更多信息。

Alternatively, you have to reinstall pip using python2 get-pip.py first and then python3 get-pip.py get-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

I had exactly the same problem!我遇到了完全相同的问题!

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

after reinstall, pip install packagename works!重新pip install packagename后, pip install packagename有效!

None of these worked for me so what I did was I navigated to "C:\\Users(User)\\AppData\\Local\\Programs\\Python" And deleted all the old python versions I wasn't using.这些都不适合我,所以我所做的是导航到“C:\\Users(User)\\AppData\\Local\\Programs\\Python”并删除了所有我没有使用的旧 python 版本。 (Worked) (工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM