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
- VS Code should be setup to use Python.
- A Python virtual environment should be created for the VS Code workspace.
- Python should be upgraded on macOS to the desired version which you want to use in the VS Code workspace. Here in my case it’s 3.8.5.
Steps to upgrade Python in workspace
- Launch the Visual Studio Code python workspace.
- 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.
- Go back to Command Palette and search and start Terminal: Create New Integrated Terminal. The terminal panel will open at the bottom.
- 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
- Restart VS Code and open the workspace.
- Now go back to the Command Palette and type in and select Python: Select Interpreter again.
- You can see the newly upgraded version (3.8.5) of python virtual environment listed.
- Select the virtual environment Python 3.8.5 64-bit (‘env’: venv).
- From now on, the VS Code workspace will use the environment with the new version of Python.
Tools & Technologies Used
- macOS Catalina (10.15.6)
- Visual Studio Code (1.48.2)
- Python (3.8.5)