Pip error after changing from bash to zsh

7,176

I saw this error message and it was an issue related to my $PATH.

Here's some troubleshooting steps.

You can view your path with echo $PATH.

which pip and which python should return the same location. Those commands search the directories in your $PATH to find the first location that contains the executable.

If you're using pyenv, which pip and which python will return the same "shims" directory but that doesn't necessarily mean the executables are being run from the same directory. In that case, use pyenv which pip and pyenv which python.

In my case, my $PATH had a ~/.local/bin/ directory that was being prepended when I ran inside tmux. ~/.local/bin/ had a pip executable but no python executable. So I was trying to use a pyenv version of Python but a ~/.local/bin/pip version of pip.

edit to clarify:

If which pip and which python show different paths, or in the case of pyenv if pyenv which pip and pyenv which python show different paths, then that may be the problem.

The solution will depend on your environment so it's difficult to give a one-solution-fits-all answer.

Maybe your $PATH has ~/.local/bin before /usr/bin and ~/.local/bin has a pip executable but not a python executable. So your computer is finding pip in ~/.local/bin but finding python in /usr/bin. In that case, you could delete the pip in ~/.local/bin if you don't need it. Or you could update it to be a symlink to the pip in /usr/bin (assuming you have another pip executable there). Or you could add a symlink of ~/.local/bin/python to /usr/bin/python. Or you could update your $PATH so that it searches /usr/bin before searching ~/.local/bin.

The solution is dependent on your environment, how you installed Python, which version you want to use, etc...

Share:
7,176

Related videos on Youtube

MarvinKweyu
Author by

MarvinKweyu

"Everybody stand back, I know regular expressions!" ─ xkcd 208

Updated on September 18, 2022

Comments

  • MarvinKweyu
    MarvinKweyu over 1 year

    So I recently changed from the bash shell to zsh. However, I have some trouble when working with python specifically pip. When I try

    pip list
    ModuleNotFoundError: No module named 'pip._internal.main'
    

    Doing which pip gives me /home/marvin/.local/bin/pip, So following a line I saw in my bashrc , I added this to zshrc

    # adding packages installed via pip
    export PATH="/home/marvin/.local/bin:$PATH"
    
    

    I also restarted shell and machine just to give a try. I am still not able to work with virtual environments with pip. Could someone guide me on a solution to this?

    [Edit]

    So I did

    python3 -m pip install --user --upgrade pip
    

    and got it working globally. Unfortunately, I had to re-create my virtual environment. The hiccup though, switching between shells would mean a re-creation of an environment. there's gotta be a better way of doing this

    • Kusalananda
      Kusalananda over 4 years
      Did you have some other pip or Python-related settings in your old bash init files tah may be relevant to how pip behaved under bash?
  • MarvinKweyu
    MarvinKweyu over 4 years
    Tried that out, would not work on zsh as it does not recognize pip. Definitely works in bash. Check the updated question?
  • MarvinKweyu
    MarvinKweyu over 4 years
    I got a /home/marvin/.local/bin/pip and a /bin/python for pyenv which ...
  • Eric Ihli
    Eric Ihli over 4 years
    Edited to clarify. In short: that they are different is what I think is the problem. Try doing whatever it takes to make those match and see if it fixes the issue. It's not easy for someone to say what you should do to make those match because it's very dependent on your particular environment.