Author's Picture
Author: Joel Gray Published: 20 December 2023 Read Time: ~3 minutes

How to create a Python Virtual Environment (venv) 

Linux

  1. Open a Terminal: Ctrl + Alt + T
  2. Ensure Python is Installed: python3 --version
  3. Create a Virtual Environment: python -m venv env
  4. Activate the Virtual Environment: source env/bin/activate
  5. Install requirements: pip install -r requirements.txt

Windows

  1. Open a Terminal: Win + R, type cmd
  2. Ensure Python is Installed: py --version
  3. Create a Virtual Environment: py -m venv env
  4. Activate the Virtual Environment: env\Scripts\activate
  5. Install requirements: pip install -r requirements.txt

Note: To leave a virtual environment, you can type deactivate in the command line and press enter, or simply close the terminal window/open a new terminal.

This tutorial requires that you have Python and PIP installed.

See our blog on installing Python here.

See our blog on installing PIP here.

Detailed Instructions:

Linux

Open a Terminal

  1. Use a keyboard shortcut, typically Ctrl + Alt + T.
  2. Alternatively, you can search for “Terminal” in your system’s application menu and open it from there.

Ensure Python is Installed

You can type the following command into the terminal and ensure the output lists a Python version of 3 or above.

python3 --version
Output of python3 --version

Create a Virtual Environment

Run the following command, which will great a Python virtual environment with the name “env” in your current directory.

Note: You can call this environment whatever you want, but common convention is “venv” or “env”.

python -m venv env

Activate the Virtual Environment

Run the following command to activate the virtual environment. This will mean that your current terminal will be running within the Python virtual environment and no longer using your default Python installation.

Note: You’ll notice the name of the virtual environment appear in brackets before your current directory in the terminal, this is an easy way to know if you’re using the virtual environment or not.

source env/bin/activate

Install Requirements

Given that we’ve just created a new Python environment we don’t have any of our previously installed Python packages as they were installed on a different Python environment. This means we need to reinstall our necessary packages again.

If you have a requirements.txt file in your current directory you can simply do the following:

pip install -r requirements.txt

Alternatively you can install your packages separately, here’s how we’d install the requests module:

pip install requests

Windows

Open Command Prompt

  1. Press Win + R to open the Run dialog, type cmd, and press Enter.
  2. Or, search for “Command Prompt” in the Start menu and select the application to open it.

Ensure Python is Installed

You can type the following command into the terminal and ensure the output lists a Python version of 3 or above.

py --version

Create a Virtual Environment

Run the following command, which will great a Python virtual environment with the name “env” in your current directory.

Note: You can call this environment whatever you want, but common convention is “venv” or “env”.

py -m venv env

Activate the Virtual Environment

Run the following command to activate the virtual environment. This will mean that your current terminal will be running within the Python virtual environment and no longer using your default Python installation.

Note: You’ll notice the name of the virtual environment appear in brackets before your current directory in the terminal, this is an easy way to know if you’re using the virtual environment or not.

env\Scripts\activate

Install Requirements

Given that we’ve just created a new Python environment we don’t have any of our previously installed Python packages as they were installed on a different Python environment. This means we need to reinstall our necessary packages again.

If you have a requirements.txt file in your current directory you can simply do the following:

pip install -r requirements.txt

Alternatively you can install your packages separately, here’s how we’d install the requests module:

pip install requests