Create a Python Virtual Environment with Built-In venv Module

Adnan Karol
The Startup
Published in
2 min readJan 11, 2021

Data Scientists or even Python Software Engineers commonly need to use python virtual environments for using dependencies, apart from those installed in their system.

Python provides the option to create virtual environments without installing any software or additional tools.

Let's get started with the following steps:

  1. In order to check the current packages installed with pip, enter the command in cmd terminal : pip list

This will show all active dependencies with their versions.

Python Dependencies

2. In order to create the new virtual environment enter the command:

python -m venv project_env

This will create a new virtual environment named “project_env”.

3. Once the environment is created, you can activate the environment using the command:

project_env\Scripts\activate.bat

This will activate the virtual environment and this can be seen in the terminal as shown in the following figure:

Python Virtual Environment

The new environment is now activated. Let us check the dependencies installed in venv project_env.

Python Dependencies in VENV

4. In order to deactivate simply enter: deactivate

5. As a Software Developer it is a good practice to include a requirements.txt file for a new person to install all dependencies. This can be easily done with the command: pip freeze

Pip Freeze Output

The output can be copied and pasted in the requirements.txt file.

6. In order to delete the virtual environment simply enter the command: rmdir project_env /s

The /s ensure to delete the venv even in case the directory is empty.

Hope this was informative.

Feel free to contact me here for doubts and here for more of my articles.

--

--