Creating Isolated Virtual Environments in Python Using virtualenv: A Complete Guide for Windows

Farhan Hai Khan
The Startup
Published in
6 min readJan 16, 2021

--

Having troubles managing your python workspace? Just updated a library and broke your old project scripts? Issues on using different package versions for different tasks? If the answer to any of these queries is a yes, then this blog post is definitely meant for you.

Python Virtual Environments (Source : Linuxize)

Why create Virtual Environments?

The Frustration is Real. (Source : FreeCodeCamp)

The motivation behind creating virtualenvs and the easiest method to do so isn’t straightforward: it has often been debatable. As you tend to dive deeper into python coding & start implementing real stuff on your own, you will get to know the absolute importance of the isolation that virtual environments provide and the various methodologies & commands that developers employ to suit their comfort in creating the same.

For starters, suppose you have a project on Mask-RCNN that uses a package TensorFlow version 1.3.0! A few months later working on an NLP project you realized that the current version of TensorFlow has been changed to 2.4.0 and hence you decide to upgrade your tensorflow(tf) using a pip installation. But wait! Your old project stops running as the newer version of tf no longer supports vital functions (such as tf.contrib) that was used extensively in your initial project! Don’t worry, virtualenv has got you covered!

Wohooooo! (Source: Tenor)

Global Packages vs. Local Packages

Have you ever wondered where your packages get installed on your PC? Well, they are installed globally: meaning that the packages are available to all users, all script files, and jupyter notebooks alike. The most common/general way to view your installed global packages are :

Displaying (Viewing) Packages :
Anaconda Command Prompt : conda -list
Command Line/Terminal : pip list

You can also save the installed packages to a .txt file as is customary while using requirements files used for installing dependency packages for a project.

Saving Packages :
Anaconda Command Prompt : conda list --export > package-list.txt
Command Line/Terminal : pip freeze > requirements.txt

For shorthand purposes now onwards we will refer to this dependency list file as reqs.txt. The format of the contents stored in reqs.txt are :

PackageName==VersionName # VerboseComments

A sample reqs.txt is :

Requirements File Sample for a Natural Language Processing Project.
Pro Tip : Only the PackageName is absolutely mandatory. While it is often wise to provide Versions along with PackageNames , it isn’t necessary. You might also add informative comments on your dependencies to remember the incorporated functionality of each package in your project.

Local Packages, however, can only be accessed when that environment source is activated. The use of these packages differ in terms of storage and activity but provide the same functionality without altering program activities.

It’s okay if you don’t grasp all of these concepts in the first go. (Source : MakeAMeme)

Talk is cheap. Show me the code.’ — Linus Torvalds

Working on an Actual Project

To simulate a real world problem, let us try to install and deploy a WebApp on your computer and then create a virtualenv from scratch. We will be using the TextGeneration repository from GitHub to setup a simple NLP Streamlit Application on your Web Browser. First open your preferred code editor (VSCode, Atom etc.) and open a new terminal/command line/powershell. It should display an output like the following :

CMD : Microsoft Windows [Version 10.0.18363.1198] (c) 2019 Microsoft Corporation. All rights reserved.Powershell :Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Try the new cross-platform PowerShell https://aka.ms/pscore6

Clone the remote repository on GitHub using git clone :

git clone https://github.com/khanfarhan10/TextGeneration.git

Navigate to the place where the Project was cloned by using the change directory command (cd). Adapt Your_Path to the downloaded folder :

cd Your_Path/TextGeneration

Your CLI (Command Line Interface) should change to something similar to :

C:\Users\Username\MorePath\TextGeneration>

Check Versions for PIP(Python Packaging Index) & Python :

python --version
Python 3.9.0
pip --version
pip 20.2.3

Note : We are using Python v3.9.0 whereas the project supports v3.7.6, this is done intentionally to encourage the reader to create Isolated Virtual Environments that can support any Python Version. So if you’re using a different python version, consider downloading v3.7.6 first!

Setting up virtualenv

You can install virtualenv globally using pip :

pip install virtualenv

Create a project environment directory with Your Awesome Project Environment Name (say TextGenEnv) :

mkdir TextGenEnv

Create a new (empty) virtual environment :

virtualenv TextGenEnv

Note : Users for other python versions may Install Python 3.7.6 first and then run the following command to choose the correct python interpreter with the correct path :

virtualenv --python=python3.7.6 TextGenEnv

Hurray! You’ve got your new Virtual Environment setup successfully!
Note : If you have problems with this step, try followng the debugging options provided at the appendix. Or if you have problems with your path try using absolute paths such as :

virtualenv — python=/opt/python-3.8/bin/python TextGenEnv

Enter into the newly created (empty) virtual environment :

TextGenEnv\Scripts\activate

You will notice a (Project Environment Name) appearing in the Command Line Interface :

(TextGenEnv) C:\Users\Username\MorePath\TextGeneration>

Note : To deactivate the activated Virtual Environment : use the deactivate command for basic python terminals and conda.bat deactivatefor anaconda activated terminal with (base)as header.

Wohoooo! You’ve now entered successfully in your virtual environment. But wait, it’s just a blank page right now!!!! Verify this as an empty Environment by listing all the modules present :

pip list
Package Version
---------- -------
pip 20.2.4
setuptools 51.0.0
wheel 0.35.1

Currently the virtualenv has setuptools, wheels and pip installed inside of it by default.
Lets install some freshly brewed modules into it! For this we will be needing a reqs.txt file noting all the dependency requirements for the project under the current working directory as discussed at the top. Once you have your reqs.txt file, go ahead and install all components with the help of pip (make sure your Virtual Environment is activated before you perform this step, for our case the name of the reqs.txt is actually ) :

pip install -r requirements.txt

Note : If you have problems with this step, try installing Microsoft C++ Build Tools (Commonly used for Cython Projects).

Bonus : Run streamlit run TextGenApp.py to enjoy the Web Application on your Web Browser with site location : http://localhost:8501/ !
AI-Based (NLP) Text Generator deployed on Streamlit.

Woohoo! Now that you have learned how to use virtualenv, get going and create something amazing💜!

Appendix - Useful Links for Debugging :

Pro Tip : You might want to include the name of your VirtualEnv into your .gitignore to ignore uploading it to GitHub. Also you can use the Python GitIgnore Template for performing the same.

Bug Found for Some Machines :

  • ‘virtualenv’ is not recognized as an internal or external command, operable program or batch file. Solution :
  • Try to run this to make your environment:
python -m virtualenv <nameOfEnv>

If you need to use a specific version of python, initialize it like this:

python -m virtualenv <nameOfEnv> -p=<C:/path/to/python/version3.x.x/python.exe>

--

--

Farhan Hai Khan
The Startup

AI Team @DSC-IEM | WoC ’20 Mentor | ML Intern @IIT KGP | Independent Researcher | Electrical Engineering | IEM Kolkata