Upgrade Python in a Virtualenv

Upgrade python in a virtualenv

Did you see this? If I haven't misunderstand that answer, you may try to create a new virtualenv on top of the old one. You just need to know which python is going to use your virtualenv (you will need to see your virtualenv version).

If your virtualenv is installed with the same python version of the old one and upgrading your virtualenv package is not an option, you may want to read this in order to install a virtualenv with the python version you want.

EDIT

I've tested this approach (the one that create a new virtualenv on top of the old one) and it worked fine for me. I think you may have some problems if you change from python 2.6 to 2.7 or 2.7 to 3.x but if you just upgrade inside the same version (staying at 2.7 as you want) you shouldn't have any problem, as all the packages are held in the same folders for both python versions (2.7.x and 2.7.y packages are inside your_env/lib/python2.7/).

If you change your virtualenv python version, you will need to install all your packages again for that version (or just link the packages you need into the new version packages folder, i.e: your_env/lib/python_newversion/site-packages)

Upgrade python in a virtual environment with '-m venv --upgrade'

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.

How do I update a Python virtual environment with `venv` (in Python 3.3+) to use a newer version of Python?

I guess what you're looking for is the --upgrade parameter.

python -m venv --help
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
[--upgrade] [--without-pip] [--prompt PROMPT]
ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
ENV_DIR A directory to create the environment in.

optional arguments:
-h, --help show this help message and exit
--system-site-packages
Give the virtual environment access to the system
site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks
are not the default for the platform.
--copies Try to use copies rather than symlinks, even when
symlinks are the default for the platform.
--clear Delete the contents of the environment directory if it
already exists, before environment creation.
--upgrade Upgrade the environment directory to use this version
of Python, assuming Python has been upgraded in-place.
--without-pip Skips installing or upgrading pip in the virtual
environment (pip is bootstrapped by default)
--prompt PROMPT Provides an alternative prompt prefix for this
environment.

You need to run it with the targeted python version, for example in this case:

python3.8 -m venv --upgrade <path_to_dir>

Assuming that python3.8 is the name of your python 3.8.0 executable.

Upgrading pycharm venv python version

You need to create a new virtual environment with the interpreter which version is 3.8.

  1. Go to Settings => Project => Python Interpreter

Sample Image


  1. Click on the vertical 3 dots, and click on "Add".

Sample Image


  1. Select Virtualenv Environment => New Environment

Sample Image


  1. Choose as base interpreter the one which version is 3.8 (the one you just installed)

Sample Image


  1. Click on "OK" => "OK"

  2. Once you have set the new interpreter, PyCharm will warn you that you need update some dependencies based on your requirements.txt file or, in this case, Pipfile.lock (I am using pipenv for this project)

Sample Image

That's it!

Updating python in anaconda virtual environment

You seem to be mixing up venv and conda. Your environment is managed by conda (which is a project separate from the Python language), whereas venv is the de facto virtual environment manager used within the Python language. I'm not even sure how venv handles the command you have provided.

If having 3.10 is a priority, I would suggest that you create a new conda environment using 3.10 and install any packages from your previous 3.6 environment. (This is also what the conda project recommends.)

how to create a venv with a different python version

You can have multiple python versions installed at the same time and you can create virtual environments with the needed version. Make sure you have installed the python version you need and then specify its location when you create the virtual environment:

virtualenv -p <path-to-new-python-installation> <new-venv-name>

Example:

virtualenv -p  C:\Users\ssharma\AppData\Local\Programs\Python\Python38\python.exe venv38

This will create a virtual environment called venv38 with Python 3.8.



Related Topics



Leave a reply



Submit