Pip suddenly using wrong version of Python

2019-01-23 07:37发布

Having a weird problem with pip on os x.

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.

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?

标签: python macos pip
4条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-23 08:08

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.

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

查看更多
你好瞎i
3楼-- · 2019-01-23 08:09

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

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

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

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. It's somewhat longer to type, but I prefer the expliciteness of it (which, I guess, follows the Zen of Python).

Note that updating 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 pip directly.

查看更多
聊天终结者
4楼-- · 2019-01-23 08:16

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

which python

Open your default pip executable script:

vi $(which pip)

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

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

#!/usr/local/bin/python3.7
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-23 08:23

I had exactly the same problem!

I reinstall python2 by brew brew reinstall python@2

after reinstall, pip install packagename works!

查看更多
登录 后发表回答