1

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!

Michael Paccione
  • 1,884
  • 2
  • 26
  • 57

2 Answers2

2

Locating in the directory with code. (Assume, ~/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

  • 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
1

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,147
  • 7
  • 15
  • 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