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.

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.

  • 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

After install virutalenv, if you create a virtual environment, it will be located inside your project. This is quite difficult to control your environment if you have many projects.

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

pip install virtualenvwrapper-win

Create sample virtualenv

mkvirtualenv project01
mkvirtualenv project02

Now your virtual environments are located at C:\Users\yourusername\Envs

lsvirtualenv

Setup VSCode

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

If you use VSCode, open new terminal

Else you can use Windows cmd

Working with virtualenvwarpper

List environment that you can workon

workon 

Access to project01 virtual environment

workon project01

After access project01 environment. Then, set project directory.

setprojectdir “Your Project Directory”
Example:

setprojectdir C:\Myprojects\project01

Repeat these steps for other projects

Access to project02 virtual environment

workon project02

Set project02 directory

setprojectdir C:\Myprojects\project02

After setting project directory for project01 and project02. Now let quickly switch between them:

workon project01 –> The working directory will be change

workon project02 –> The working directory will be change

Let install some packages on project01

Switch to project01

workon project 01

Install Django

python -m pip install Django

List installed packages

pip freeze

Switch to project02 to check installed packages

workon project02
pip freeze

You can see that there is no package on project02

Some usefull Python Scripts for Operation:

https://hijackson.com/how-to-schedule-a-python-script-for-system-health-check/

Leave a Reply

Your email address will not be published. Required fields are marked *