Assign a Specific Python Version to a Virtual Environment

Check Python Versions

  First we need to figure out what versions of python we have. I started with 3.11.

PS    py --list

Download Python

  Next we need to download the version of Python we need for our specific project. Once the file is downloaded, follow the installation prompts.

Check Download

  After the download is complete, we can check that we have the additional version of Python installed. We should see both versions listed, and a star next to the active version.

PS    py --list

Navigate to Location

  Next we need to navigate to the directory we want to house our virtual environment.

Create Virtual Environment with a Different Python Version

  To create a virtual environment with a specific version of Python, we need to add one additional flag to the normal command, and that is to indicate the version number.

PS    py -3.10 -m venv .venv

Compare Python Versions in and out of the Virtual Environment

  We can compare which version of Python exists as our default and which exists in our virtual environment by checking the version before and after we activate our virtual environment.

PS    python -V
>       *our default python version*

# In cmd.exe
      venv\Scripts\activate.bat
      
# In PowerShell
      venv\Scripts\Activate.ps1
      
# In Linux and macOS
      source myvenv/bin/activate

(.venv ) PS    python -V
>       *our virtual environment python version*