What is a virtual environment?

A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.

This information was taken from the official Python documentation which is rather cryptic but delivers the idea.

Basically, a virtual environment is when your Python library installations in said environment do not affect your so-called real environment (your local computer) and is basically an instance of the Python interpreter you can use solely for the project you are working on without affecting the global system-wide Python interpreter.

They are incredibly useful when it comes to generating requirements.txt files (you can use a library called pipreqs for this instead of using the popular alternative of pip freeze > requirements.txt) for your projects such that it is clone-friendly. Otherwise, it would be very difficult to generate a requirements.txt file if you are using the global interpreter as you may have unneccessary installations added to the list in the file.

The Nitty Gritty

So, let’s get into how to setup a virtual environment using a library called virtualenv (installation command: pip install virtualenv) as it is the most popular library used for the purpose of making virtual environments.

Open up your native command line application (Terminal, Command Prompt etc.)

Make a folder at any directory (for e.g Desktop):

cd Desktop
mkdir MyWebServer

you should see a folder named MyWebServer show up in your Desktop

you should see a folder named MyWebServer show up in your Desktop

Open up Visual Studio Code at that folder.

Ensure you have Python3 or higher installed on your computer.

💡 Macs usually come with Python 2.7 installed which is a legacy python version that comes pre-installed with macOS. This version is not suitable for making a web server and most modern projects.

Ensure that you have the Python extension installed in VSCode.

Ensure that you are working with the right python interpreter by checking the bottom-left of the window.

at time of writing, I am using [pyenv](https://github.com/pyenv/pyenv) to manage my python interpreters. I am using 3.10.0 as my chosen interpreter.

at time of writing, I am using pyenv to manage my python interpreters. I am using 3.10.0 as my chosen interpreter.

If the wrong python version is being displayed, click on this label and set it to the right python version from the dropdown menu.

Open up terminal in VSC. You will need to ideally have a virtual environment to make your python web server or other project such that it can be easily exported to other sources or be used by other people in general.

In your terminal, run pip install virtualenv

After virtualenv is created, you can create a new virtual environment by running:

virtualenv myvirt

you may replace myvirt with any other name of your choice but do note that it must not have any numerals or whitespaces

Then, you have to activate the environment by running either one in your command line based on the circumstances:

// For windows
myvirt\Scripts\activate

// For macOS/Linux
source myvirt/bin/activate

You should then see a new (myvirt) label before your (base) label in the command line now, indicating that you are in a virtual environment.

For managing your virtual environment, there are more details here.


Thanks for reading this post! If you liked it, do tell by contacting me at any of the links below!