Issue
I have multiple python versions managed by pyenv. I want to upgrade one of my virtual environments from 3.7.13 to 3.10.3 with the ‘—upgrade’ option as:
>deactivate
>pyenv local 3.10.3
>python3 -m venv --upgrade .venv
>. .venv/bin/activate
> python -V
Python 3.7.13
I expect the '—upgrade' would change the python version to 3.10.3 but it did not it stayed with 3.7.13
I understand it may be easier just discard and recreate the virtual environment, but I really want to learn how '—upgrade' should work
Solution
If you read the official documentation of the venv
module, then the description of the --upgrade option is very specific: "... assuming Python has been upgraded in-place." I think this implies that it has to be the same Python installation that you originally created the virtual environment with, for the --upgrade
flag to work. Each version of Python installed by pyenv is installed separately, so I wouldn't expect the --upgrade
flag to work in this case.
That being said, as far as I know, venv
does little more than installing a couple of basic scripts and configuration files, and some bunch of symbolic links. The source code of the venv
module seems fairly straightforward, and all that the --upgrade
switch does is skip the setup scripts. I think you could manually "hack" your way through this by changing some symbolic links and changing some directory names here and there. However, it's not how venv
should be used.
So, yeah, save yourself the misery, and discard the old virtual environment and just build a new one.
Answered By - Alan Verresen Answer Checked By - David Goodson (WPSolving Volunteer)