简体   繁体   中英

pip and python referring to different interpreters

I know "multiple-versions-of-python-mess" is nothing new but my question is more specific. I'm learning how to use venv (and pyenv, etc.) and I've run into a strange situation.

I have a number of different versions of python installed (as one does). I use one of them, 3.9, to create a venv:

$ /usr/local/Cellar/python@3.9/3.9.12_1/bin/python3 -m venv ./venvpractice

Then I activate it:

cd venvpractice
$ source bin/activate

Here's where the "trouble" starts:

(venvpractice) $ which python
~/venvpractice/bin/python

(venvpractice) $ which python3
python3: aliased to /usr/local/bin/python3

(venvpractice) $ which pip
pip: aliased to /usr/local/bin/pip3

(venvpractice) $ which pip3
~/venvpractice/bin/pip3

Wouldn't you expect pip and python to match (ie be from the same place), and for pip3 and python3 to match as well? Why are they all jumbled up?

I know it's not the biggest deal. I should just be careful and make sure I call the correct one, say, when I do pip3 freeze > requirements.txt . But I just want to understand what's going on under the hood. I feel like things are still mucked up. So many versions and aliases and symlinks and PATH variables in /.zshrc ... And then there's pyenv with which I also experimented a bit...

Please help?!

venv will not unalias anything for you; if you have aliases, your shell interprets those before the venv stuff even gets a chance.

My simple recommendation would be to remove these aliases from your shell's startup files, or at least interactively unalias them temporarily.

If you need for there to be an alias, try something like

alias python3='env python3'

which should do the right thing both in a virtual environment and out of one (provided your PATH is moderately sane, and you have /usr/local/bin/python3 pointing to /usr/local/bin/python ).

(Though in other news, aliases are inferior to shell functions. For this simple case, mmmmaybe live with an alias.)

A slightly more elaborate function might look like

python () {
    case ${VIRTUAL_ENV-} in
      '') /usr/local/bin/python "$@" ;;
      *) command python "$@";;
    esac
}

But ultimately, if you are using pyenv anyway, probably just get out of its way and let it handle these things for you; it does that nicely and transparently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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