Pip suddenly using wrong version of Python
Asked Answered
D

8

34

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?

Dashpot answered 30/3, 2016 at 14:8 Comment(4)
Any chance another program (like an install script) pre-pended something to your .bashrc or linked pip to pip3?Neisa
What version of OSX are you running? Where did you get that python from? Homebrew? Are you sure you didn't update something?Biretta
That's possible, but I don't see anything in my .bash_profile (and don't have a .bashrc) I run brew update and brew upgrade about daily. OS X version is 10.11.4Dashpot
If you are on Mac OS X and update python with brew. It seem to knock out pip. Its has happened more than once for me.Moonstruck
B
48

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.

Bergmann answered 2/6, 2016 at 8:18 Comment(3)
+1 This is a better solution than worrying about what version of Python pip will try to use implicitly when you have both Python 2 and 3 on a system. Should be the accepted answer imhoZarathustra
Instead of python3 -m pip search <package> use python3 -m pip install <package> as "pip search" is depreciated. See status.python.org for more information.Eximious
@Eximious : Kindly suggest it as an edit, instead of a commentTeheran
O
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
Orgy answered 2/6, 2016 at 8:11 Comment(5)
This did not work for me. pip still had some other internal reference to the wrong python version, fwiw.Wellborn
my answer is based on my local setup, i will fix it. @Wellborn first see which pip is your default pip by running: "which pip". To see which python your pip is using run: "head -1 $(which pip)" Also this fix may be invalid since it worked for me in 2016, but pip changed significantly since then.Orgy
@Wellborn i just updated the answer, i tested the steps and those are good for recent pip (ver 18.1) and python 3.7Orgy
@DmitryTokarev: But the problem here is like in one of the other answers, now I do python3.7 -m pip -V, it will spit out pip version xx.xx for python 2.7 and hence it has ruined pip3.7, which may even be the default pip3 for a userTeheran
Also, why not just set update the symlink to /usr/local/bin/pip?Teheran
M
5

Try setting 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

Mate answered 30/3, 2016 at 14:21 Comment(3)
Their problem is that /usr/local/bin/pip is now linked to pip3 so I don't think your solution will work.Biretta
He needs to reinstall pip then as suggested in the second part of the answer.Mate
Will reinstalling pip require me to reinstall all of the libraries I have used pip to install?Dashpot
M
4

I had exactly the same problem!

I reinstall python2 by brew brew reinstall python@2

after reinstall, pip install packagename works!

Mccollough answered 29/5, 2018 at 13:20 Comment(1)
For whatever reason, I had to do brew uninstall python@2 then brew install python@2. reinstall didn't work.Wellborn
C
0

None of these worked for me so what I did was navigate to

C:\Users(User)\AppData\Local\Programs\Python\

and deleted all the old python versions I wasn't using. (Worked)

Cattycornered answered 3/11, 2021 at 19:13 Comment(0)
R
0

My pip also was aimed at the wrong Python (I want 310). Python310 -m pip install wasn't working for me because python310 was not a variable in my environment. Instead I realized I need to write out the path (in my case its C:\Python310\python.exe) and then in command prompt do

PATH -m pip install <package>

which for me is

C:\Python310\python.exe -m pip install <package>

its extra typing, but it works perfectly.

Now need to figure out how to reconfigure my pip to default back to Python310...

Remontant answered 25/4, 2023 at 16:25 Comment(0)
M
0

If anyone here happens to be using Windows 10/11...

and installed a new version of Python but your pip is misaligned:

Check your environment variables.

It's likely that you might've checked the "Add to Path" option when installing your Python version (which I generally do because it's convenient haha).

1. Run Windows search Win or Win+S and search the following:

Edit environment variables for your account

Click the option that pops up. You should now see 2 boxes containing your environment variables, one for User, and for System.

In the top list box (user environment variables) there should be an item that says "Path" (not in uppercase).

2. Double click the "Path" option and you should see some paths that look like this:

C:\Users\yourusername\AppData\Local\Programs\Python\Python312\Scripts\
C:\Users\yourusername\AppData\Local\Programs\Python\Python312\

3. If you see another option that has your old Python version (Python 311, etc.), just delete it and DON'T delete the new ones :)

And that's it!


If you accidentally delete both, just add the same as above:

C:\Users\yourusername\AppData\Local\Programs\Python\Python3x\Scripts\
C:\Users\yourusername\AppData\Local\Programs\Python\Python3x\

and replace whatever the "x" is on "Python3x" with the actual version of Python you want it to user.

For example, for Python 3.12:

C:\Users\yourusername\AppData\Local\Programs\Python\Python312\Scripts\
C:\Users\yourusername\AppData\Local\Programs\Python\Python312\

3.11:

C:\Users\yourusername\AppData\Local\Programs\Python\Python311\Scripts\
C:\Users\yourusername\AppData\Local\Programs\Python\Python311\

etc. Otherwise you can also just redownload Python and click the "Add to Path" checkbox again

Minimal answered 20/3 at 6:51 Comment(0)
L
0

To change the default version (of pip, python and other command-line tools), when you already have multiple versions installed, consider editing the $PATH variable.

On Linux-based systems (including Mac), you can see the current value of $PATH with env | grep PATH or echo $PATH.

Default values of $PATH are set in either of these files. They're either plain-text files or soft links to plain-text files, so can be edited with vi, nano or other simple text editors. Check both for what's setting up your current $PATH, and modify the one relevant to your needs:

  • /etc/paths : Provides system defaults. You may need to open a new terminal to see the changes.
  • ~/.bash_profile, ~/.zprofile, or similar depending on your shell: Provides defaults for the current user. You'll need to open a new terminal or source ~/.zprofile to see the changes.
Leaseholder answered 25/4 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.