Creating Virtual Environments using Python’s built-in venv module on Windows.

Peter Ojo
3 min readDec 30, 2020

--

https://morioh.com/p/8da4f9ea1914
Image source: https://morioh.com/p/8da4f9ea1914

The venv the module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.

Create a project folder.

C:\Users\Therock\Desktop>mkdir myproject

Change directory to the project folder.

C:\Users\Therock\Desktop>cd myproject

To view all Python’s packages in the system, type in the command below in any directory. It will display all the packages already installed on the system.

C:\Users\Therock\Desktop\myproject>pip list

To create a new virtual environment on desktop

C:\Users\Therock\Desktop\myproject>python -m venv my_venv

A new folder named “my_venv” is created in “myproject” directory. To verify, type “dir”, and it will display all the items in the directory.

C:\Users\Therock\Desktop\myproject>dir

Activate the virtual environment.

C:\Users\Therock\Desktop\myproject>my_venv\Scripts\activate.bat

The output displays the virtual environment name beside the directory as seen below. This shows that the environment is activated.

(my_venv)C:\Users\Therock\Desktop\myproject>

You can also verify the path to the current python command by typing the command below in the environment. The virtual environment directory will be listed at the top of the output. When creating the environment, use the version of python you will use in the environment to create it. If the environment was created in python3.7, then the environment will use python3.7.

(my_venv)C:\Users\Therock\Desktop\myproject>where python

You can now view all the packages installed in this environment. Since it is a new environment, you will only see “pip” and “setuptools” installed.

(my_venv)C:\Users\Therock\Desktop\myproject>pip list

You can now install packages of your choice in the environment using “pip install <package-name>”. E.g. Using the “pip list” command again, you will see that more packages will be displayed.

(my_venv)C:\Users\Therock\Desktop\myproject>pip install requests

To export all the installed packages, we can do this by saving all the installed packages in a “requirements.txt” file that you have already created. Copy the output of the “pip freeze” command into the file.

(my_venv)C:\Users\Therock\Desktop\myproject>pip freeze

To deactivate the environment, the output omits the environment name as seen in the second line below. This shows that it has been deactivated.

(my_venv)C:\Users\Therock\Desktop\myproject>deactivateC:\Users\Therock\Desktop\myproject>

To delete the environment entirely, use the command below. You will see a prompt, enter “Y”. If you do not include “/s”, you will not be able to delete the entire folder tree. You can also delete the folder without using the cmd by merely dragging the folder into a trash bin or using “shift + delete” from the project directory.

C:\Users\Therock\Desktop\myproject>rmdir my_venv /s

To create a new virtual environment and also installing the packages saved in the “requirements.txt” file.

C:\Users\Therock\Desktop>python -m venv myproject\venv

or below, if you are still in the project directory.

C:\Users\Therock\Desktop\myproject>python -m venv venv

Activate the environment

C:\Users\Therock\Desktop\myproject>venv\Scripts\activate.bat
(venv) C:\Users\Therock\Desktop\myproject>

Install the requirements saved in the project directory in the new environment.

(venv) C:\Users\Therock\Desktop\myproject>pip install -r requirements.txt

Now, you should see all the packages have been installed in the new environment.

(venv) C:\Users\Therock\Desktop\myproject>pip list

To create a new virtual environment that has access to all python packages installed on the system, use the command below.

C:\Users\Therock\Desktop\myproject>python -m venv venv --system-site-packages

Activate it and use pip list to show all these packages.

C:\Users\Therock\Desktop\myproject>venv\Scripts\activate.bat
(venv) C:\Users\Therock\Desktop\myproject>
(venv) C:\Users\Therock\Desktop\myproject>pip list

You can install a package that is not on the system but you need only for this project into this environment. e.g If “Pandas” package is not on the system, install it and view it using the “pip list” command. It will be included in the list. If you deactivate this environment and use the “pip list” command again, you won’t see the “panda” package. This means that it’s only installed for a particular environment.

(venv) C:\Users\Therock\Desktop\myproject>pip install pandas

To view only packages installed in this particular environment, use command below. You can also use “pip freeze — local” to display packages for the particular environment.

(venv) C:\Users\Therock\Desktop\myproject>pip list --local

Addendum

  • It is not a good practice to save other files of the project in the environment folder. Always save other files in a separate location within the project directory.
  • When uploading a project to platforms like GitHub, you don’t need to upload the environment folder. Only save the installed packages in a “requirements.txt” file and upload this file. Others can install all packages used for the project from there.

References

--

--

Peter Ojo
Peter Ojo

Written by Peter Ojo

Cloud Engineer. Documenting my learning.

No responses yet