Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upgrade Python venv version [duplicate]

Tags:

python-venv

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!

like image 746
Michael Paccione Avatar asked Jul 11 '26 02:07

Michael Paccione


2 Answers

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
    
like image 182
Savostyanov Konstantin Avatar answered Jul 17 '26 17:07

Savostyanov Konstantin


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
like image 23
mnikley Avatar answered Jul 17 '26 16:07

mnikley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!