5

I installed Python3.10 and I have a venv in a project I have been working on. I don't understand how to upgrade this easily. My background is mostly in Node and JS which is must simpler and more straightforward to change versions.

I was just trying to create a new venv but that didn't work

mpaccione@T430:~/Projects/investing/react-flask-app/server$ python3.10 -m venv ~/Projects/investing/react-flask-app/server
Error: Command '['/home/mpaccione/Projects/investing/react-flask-app/server/bin/python3.10', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

I also thought maybe I could change the pyvenv config could be changed but that didn't work either

home = /usr/bin
include-system-site-packages = false
version = 3.8

to

home = /usr/bin/python3.10
include-system-site-packages = false
version = 3.10

Is there just a simply straightforward way to change this? I'm sure it's a common use case!

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Michael Paccione
  • 2,467
  • 6
  • 39
  • 74

2 Answers2

9

Locating in the directory with code.
Assuming ~/Projects/investing/react-flask-app/server for you.

  1. Activate venv if it isn't (assume, venv using for current virtual environment):

    $ source venv/bin/activate
    
  2. Save your current dependencies:

    $ pip freeze > requirements.txt
    
  3. Deactivate current virtual environment:

    $ deactivate
    
  4. Delete the current venv folder (I don't how it is called in your machine).

  5. Create a new venv folder (if python3.10 using for Python 3.10):

    $ python3.10 -m venv venv
    
  6. Activate venv:

    $ source venv/bin/activate
    
  7. Install saved dependencies:

    $ pip install -r requirements.txt
    
informatik01
  • 16,038
  • 10
  • 74
  • 104
  • When I go to create a new venv i get an error code of exit status 1... yet I know python3.10 is installed and does work when I type python3.10 it runs the python >>> – Michael Paccione Feb 16 '22 at 02:30
4

According to the official docs venv has an --upgrade command, so if your interpreter-hook of the most current version is python3.10, it should be:

python3.10 -m venv --upgrade ~/Projects/investing/react-flask-app/server

However, it might be that this fails because your hook-alias changed and that the python installation is different from the one used to create the venv. From the docs regarding --upgrade: Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place.


I would recommend to simply store the dependencies in a requirements.txt file, delete the venv, and recreate it with the new python version to make a clean new venv:

  1. Activate the old venv
  2. Store the dependencies in a file with python -m pip freeze > requirements.txt
  3. Delete the old venv folder, keep the requirements file
  4. Create a new venv & activate it
  5. Install requirements with: python -m pip install -r requirements.txt
mnikley
  • 1,625
  • 1
  • 8
  • 21
  • I still get the same exit error code on the install and in trying to create a new env. I'm quite stuck and already wrote some switch statements in Python which are only supported in 3.10 :| – Michael Paccione Feb 16 '22 at 02:31
  • In my particular case (migrating an existing `Python 3.10.12` venv to `Python 3.11.4` venv) failed during `python3.11 -m venv --upgrade /srv/homeassistant` with the following error message: ("`Error: Unable to create directory '/srv/homeassistant'`"). The cause of this failure seems to be that in my case `/srv/homeassistant` is set up as a symlink to `/mnt/otherpartition/srv/homeassistant`. Using the actual non-symlinked / resolved path resolved the issue: `python3.11 -m venv --upgrade /mnt/otherpartition/srv/homeassistant` (silently finished after a couple of seconds, with exit status 0). – Abdull Aug 06 '23 at 01:59