How to use Python Virtualenv and Virtualenvwrapper on Windows

How to use Python Virtualenv and Virtualenvwrapper on Windows? This guide will show you how to Install Python Virtualenv and Virtualenvwrapper. Virtualenv is a tool for creating isolated Python virtual environments, each with their own libraries and site-packages.

Requirement

  • Python3 Installed

First, open CMD and check Python version

python -version

If you can not find python version. Please install python3: https://www.python.org/downloads/

Install python virtualenv using pip

pip install virtualenv

Install virtualenvwrapper for Windows

Virutalenvwrapper will help you to organize your environment in one place and control it easily.

pip install virtualenvwrapper-win

Optional: Setup VSCode

If you use VScode, you need to set default terminal is Command Prompt (cmd). Virtualenv does not function correctly in Windows Powershell

Press F1 –> Type Terminal: Select default profile –> Choose Command Prompt

Open new terminal in VScode

Working with virtualenvwarpper

Virtualenvwrapper is a set of extensions to virtualenv for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

You can open VScode Terminal (cmd) or using Windows CMD. Personally, I will use VScode Terminal

1. Create sample virtualenv

mkvirtualenv project01
mkvirtualenv project02

Virtual environments are located at C:\Users\yourusername\Envs. All virtual environments are place in one place. That’s awesome !!!

lsvirtualenv

2. List current virtual environment

workon

You can easily see current virtual environment

3. Access specified virtualenv

Access virtualenv by using command: workon “virtual-environment-name”.

Example: Access virtual environment named: project01

workon project01

4. Set virtualenv directory

At this point, we did not set directory for this virtual environment. So you need to change to the directory every single time when you switch to different virutalenv. However, we can make it more easy by set directory for virtualenv, then your directory will be automatically when you access to any virtualenv.

After access project01. We set virtualenv directory: setprojectdir “Your Project Directory”

For example: C:\Myprojects\project01 is my Django Project

setprojectdir C:\Myprojects\project01

This command will set C:\Myprojects\project01 to Virtualenv named: Project01

Set directory for virutalenv named: Project02

workon project02
setprojectdir C:\Myprojects\project02

Verified that project directory change when you access any virtualenv

workon project01
>>> The working directory will be change
workon project02
>>> The working directory will be change

Repeat step 1 to 4 for any virtualenv you need.

4. Test virtualenv packages isolation

Let install Django packages on project01 virtualenv

Switch to project01 virutalenv

workon project01

Install Django package on project01 virutalenv

python -m pip install Django

List installed packages

pip freeze

Switch to project02 virutalenv to check installed packages

workon project02
pip freeze

You can see that there is no package on project02

Conclusion

In this tutorial you setup virtualenv for isolated installed packages for every single projects. By default, the virutalenv need to located in your project folder and it will be a nightmare for you to manage if you have hundred of projects.

Virtualenvwrapper helps you to orginize virutalenv in one place and manage them easily.

More Reading

Post navigation

Leave a Reply

Your email address will not be published.