0

I've used pyenv to install Python 3.8.11 and 3.9.6 on my mac (Big Sur os) and set 3.8.11 to be the version of Python that I want to use for creating a virtual environment (followed the instructions here). But, even though I've set up system to use 3.8.11, the virtual environment that results seems to be using Python 3.8.2.

This is what I did:

  1. Install pyenv via Homebrew

  2. Install Python 3.8.11 and 3.9.6 via pyenv install 3.X.X

  3. Added eval "$(pyenv init -)" to ~/.bash_profile which was an empty file

  4. Typed pyenv local 3.8.11 in the terminal and then pyenv versions in the terminal to confirm that 3.8.11 was being used:

    $ pyenv versions    
    system
    * 3.8.11 (set by /Users/me/projects/sample/.python-version)
    3.9.6
    

But, when I typed python3 -V in the terminal, my system indicates that Python 3.8.2 being used:

$ python3 -V
Python 3.8.2

When I type which python3, the system shows the path to python3 (presumably 3.8.2) as /usr/bin/python3:

$ which python3 
/usr/bin/python3

I don't recall when/how Python 3.8.2 was installed on my machine. I did try to uninstall with brew uninstall python@3.8 but the system returned:

$ brew uninstall python@3.8
Error: No available formula or cask with the name "python@3.8". Did you mean python@3.8, python@3.9, python@3.7 or python-yq?

Taking a gamble anyways because I was stuck, I typed python3 -m venv virtual_env to create my virtual environment, hoping that the new environment would be the version of python that I wanted but after activating my virtual environment using source virtual_env/bin/activate, I entered python -V and confirmed that the resulting environment was using Python 3.8.2 instead of 3.8.11:

(virtual_env) me$ python -V
Python 3.8.2 

Can somebody tell me what I'm missing/doing wrong? What's the best way to get past this problem?

Vee
  • 1,821
  • 3
  • 36
  • 60
  • don't know if it changes much but have you tried `brew uninstall python@3.8.2`? – Matiiss Aug 15 '21 at 08:31
  • `brew uninstall python@3.8.2` results in `Error: No available formula or cask with the name "python@3.8.2". Did you mean python@3.8, python@3.9 or python@3.7?` – Vee Aug 15 '21 at 14:13

1 Answers1

0

pyenv governs the python command. If you are explicitly invoking python3, you are bypassing pyenv.

A simple arrangement to allow you to use either interactively is

alias python3=python

in your shell's startup file.

Inside an active virtual environment, python and python3 both resolve to whatever that environment was set up to use. If it's not correct, just remove and (this time, correctly) recreate the environment.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Just added `alias python3=python` to `~/.bash_profile`, restarted my terminal, and tried setting the system python to 3.8.11 by typing `pyenv local 3.8.11`. But now, `python3 -V` `python -V` both show that `python 2.7.16` is the version of python being used. Any other ideas? – Vee Aug 15 '21 at 14:25
  • Sounds like `pyenv` is no longer active then. Did you remove `eval "$(pyenv init -)"` from your login files? If you run it interactively, do things start to work in that shell instance? – tripleee Aug 15 '21 at 14:28
  • I just removed `eval "$(pyenv init -)"` from `~/.bash_profile` and same result... system is still using python 2.7.16. I did try running my flask app with `flask run` and I get an error because of using the wrong version of python. – Vee Aug 16 '21 at 04:40
  • No, don't _remove_ it. I'm asking to put it back if you did, and figure out why `pyenv` is not active if you thought it was going to be. – tripleee Aug 16 '21 at 04:44
  • OK, so the problem was that my system wasn't using the path to the pyenv shims. Reason: I had `export PATH="$HOME/.pyenv/bin:$PATH"` instead of `export PATH="$HOME/.pyenv/shims:$PATH"` in `~/.bash_profile`. Not sure how that happened but it was prob from me getting desperate and just pasting all kinds of wrong info from researching this issue for 6 hrs. The solution listed didn't address my problem but your most recent comment directed me to find the solution. Happy to give select your solution if you want to update your answer. Thanks for your help! – Vee Aug 16 '21 at 05:57
  • Probably more useful to just delete the question; this is unlikely to help future visitors. – tripleee Aug 16 '21 at 06:01
  • Agree that the question, as presented at the moment, would not help others. I'll edit it to be more generic: issue is that pyenv is not setting the desired python version and the solution is that my PATH did not include the shims directory. Again, thanks for your help! – Vee Aug 16 '21 at 06:10