21

I have recently installed Python 3.8.0 alongside Python 3.7.4.

I have some virtual environments (created using python -m venv <directory> that are based on v3.7.4. How do I update them to use v3.8.0?

Do I need to create a new virtual environment and reinstall the dependencies, scripts, etc.?


Note: There are some existing Q&A's (such as this) that deal with the older virtualenv package/tool. I'm specifically asking about the new built-in venv module, which is a standard built-in to Python since v3.3 and has some differences from virtualenv.

LightCC
  • 9,804
  • 5
  • 52
  • 92
  • Possible duplicate of [How to change the python version of already existing virtualenv?](https://stackoverflow.com/questions/51915484/how-to-change-the-python-version-of-already-existing-virtualenv) – gstukelj Oct 25 '19 at 17:16
  • 3
    Possible duplicate of [Can existing virtualenv be upgraded gracefully?](https://stackoverflow.com/questions/2170252/can-existing-virtualenv-be-upgraded-gracefully) – jeremycg Oct 25 '19 at 17:17
  • Do you *need* to? Maybe not. *Should* you? Yes. – chepner Oct 25 '19 at 17:18
  • @gst, @jeremycg - these answers deal with the older `virtualenv` module/package. I'm only interested in the newer `venv` that is now built-in, with different usage. Updated to specify. – LightCC Oct 25 '19 at 17:23
  • @chepner Whether you need to or should depends on other project requirements and cannot be generically answered. – LightCC Oct 25 '19 at 17:23

2 Answers2

14

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.

RMPR
  • 3,368
  • 4
  • 19
  • 31
  • This looks correct - what is the usage though? How do I specify which version to update to (for example)? – LightCC Oct 25 '19 at 17:25
  • something like `python3.8 -m venv ...` – RMPR Oct 25 '19 at 17:26
  • Sounds great. But I have some wrong with it: F:\MyCodes\python\dtprjops>python -m venv --upgrade venv Error: [Errno 13] Permission denied: 'F:\\MyCodes\\python\\dtprjops\\venv\\Scripts\\python.exe' – Allis Gao Jun 06 '21 at 12:33
  • @AllisGao You need to run it in an administrator command prompt – RMPR Jun 07 '21 at 16:27
  • So using vscode, you would not activate the venv and then `python -m venv --upgrade .venv` – Mike Wise Nov 15 '21 at 22:37
  • `--upgrade-deps` is also worth doing – Mike Wise Nov 15 '21 at 22:38
  • 4
    I don't see how `--upgrade` is useful since it doesn't 'transfer' the old site-packages to the new Python version site-package directory. Thus, you still have to reinstall all the dependencies ... So what's the point? – maxschlepzig May 08 '22 at 12:27
  • @maxschlepzig is right. I had to manually copy the site-packages folder to the new version. Even then, some libraries gave errors: like pyzmq. I uninstalled and reinstalled them and now it is working. – sha May 10 '22 at 22:12
  • 1
    "run it with the targeted python version" solved it for me – Parzival Jul 05 '22 at 06:57
  • For the record, I came to the conclusion a while ago that just rebuilding the venv is easy enough as long as you have access to pypi via the web. However, in the last year I have started using the Poetry package to manage my Python projects, which adds a lot of powerful capabilities around dependency management, and once you make the updates a simple `poetry install` will automatically update or recreate the venv. – LightCC Nov 03 '22 at 15:23
  • @LightCC I also like poetry, it works amazingly well, except when it doesn't. For example with my side project [atbswp](https://github.com/rmpr/atbswp) I tried using poetry, but it fails building [WxPython](https://github.com/wxWidgets/Phoenix). I'm pretty sure that there are other projects out there using Python bindings for a low level language (like C or C++) that you might have issues using Poetry with. – RMPR Jan 25 '23 at 13:01
  • @RMPR, as with anything, there are exceptions. So when we run across those (in our case, some older packages that don't meet all PEP standards and require compilation), we just make an install guide or script for that project, which only needs to be run when the problem package is updated. Still get all the other goodness for all other dependencies. Happy to consider any better alternatives you have found... – LightCC Jan 30 '23 at 07:11
3

from a command prompt outside your venv, when VScode is not running.

python -m venv --upgrade --upgrade-deps "c:/your/project/folder/.venv"

some of the packages are not upgraded correctly/successfully so fixed these with pip uninstall and reinstall.

pip uninstall pyodbc
pip install pyodbc
ozhug
  • 983
  • 11
  • 19