Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 (i.e. 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?!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading