Upgrade Python Virtual Environment

Upgrade python version in visual studio code python workspace

In my earlier article, I have explained how to upgrade Python on macOS and setup Visual Studio Code project for Python development. Now we will see how to upgrade the already setup Python environment in VS Code workspace.

In my current Python workspace, the interpreter is pointing to a virtual environment with Python 3.7.4. To upgrade this to version 3.8.5, I’ve followed these steps.

Caution

When upgrading the python interpreter version in python workspace, you may encounter errors or warnings while running the project because of incompatible usage of methods or objects. So please review the release documents from python.org before upgrading and prepare an action plan to migrate the code to new version of Python.

Prerequisites

Steps to upgrade Python in workspace

  1. Launch the Visual Studio Code python workspace.
  2. Check the current python virtual environment version. You can check this by opening the Command Palette (by pressing Shift, Command and P keys) and select Python: Select Interpreter. This will show you the list of interpreters, both global and virtual. In my case Python 3.7.4 64-bit is the current virtual environment and 3.8.5 is listed as global environment.
    Check the current version of python virtual environment
  3. Go back to Command Palette and search and start Terminal: Create New Integrated Terminal. The terminal panel will open at the bottom.
  4. In the terminal run the below comment. In this comment python3.8 is important to upgrade the environment with Python 3.8.x. env is the existing name of the virtual environment.
python3.8 -m venv --upgrade env
source env/bin/activate
  1. Restart VS Code and open the workspace.
  2. Now go back to the Command Palette and type in and select Python: Select Interpreter again.
  3. You can see the newly upgraded version (3.8.5) of python virtual environment listed.
    Upgrade Python Virtual Environment
  4. Select the virtual environment Python 3.8.5 64-bit (‘env’: venv).
  5. From now on, the VS Code workspace will use the environment with the new version of Python.
    Select the new version as the interpreter

Tools & Technologies Used

  • macOS Catalina (10.15.6)
  • Visual Studio Code (1.48.2)
  • Python (3.8.5)


Related Articles & Tools

Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.