Getting Started With Virtual Environment

Subscribe to my newsletter and never miss my upcoming articles

Consider a scenario where one is working on multiple Python projects that depends on different versions of the same packages (I wrote about packages here) or a project that must be isolated from certain packages because of a namespace collision or you’re probably working in a highly controlled environment like a managed hosting or a server where the interpreter or packages cannot be changed as a result of production requirements or maybe you simply want to experiment with specific combination of packages in controlled circumstances, guess what? Python virtual environment got you covered.

What exactly is Python virtual environment? 🤔

A virtual environment is basically an isolated environment or space for your Python projects. This simply means that each project can have its own dependencies (packages for example), regardless of what dependencies every other project has. There are also no limits to the number of environments you can have since they’re just directories containing a few scripts. Each environment is specific to a project.

How does it work?

The virtual environment tool creates a folder inside the project directory. It keeps Python and pip executable files inside the virtual environment folder. When the virtual environment is activated, the packages installed after that are installed inside the project-specific virtual environment folder. If you are using Python 3, you should already have the venv module from the standard library installed. Otherwise, you’ll have to install the virtualenv tool with pip (Python package installer). Using the command prompt;

pip install virtualenv

Start by making a new directory to work with if you haven’t already done so, that is creating the project folder. On the command prompt, navigate into the location you want your project saved with cd (that is; documents, desktop, downloads, etc.).

cd desktop
mkdir <projectname>
cd <projectname>

Create the virtual environment using the following command;

Virtualenv .  #the virtual environment directories will be created directly in the project folder
Or
Virtualenv <name>  #this creates the virtual environment  in the <name> folder

The whole process of setting up the virtual environment may take a minute or two. When it’s finished, you should have a directory with a few subdirectories in it. The most important subdirectory is Scripts on Windows or bin on Linux or Mac, which is where you’ll find a copy of the Python interpreter for the virtual environment along with its utilities.

Activating the virtual environment

Before you can use the virtual environment, you need to activate it. Activation makes the virtual environment the default Python interpreter for the duration of the session. Different syntax is used for activating the virtual environment depending on which operating system and command shell you’re using.

#using Windows OS

Scripts\activate   #use this if you created the virtual environment with the first command 

<name>\Scripts\activate   #use this if you created the virtual environment with the second command

source env/bin/activate   #using Linux or Mac

pic1 (2).png Here’s mine 😉, notice how the virtual environments’ name now appears in parenthesis before the project path, this means the virtual environment has been successfully activated.

congrats.png Photo: Shutterstock

Also, note that <projectname> refers to the name of your project, it can be named as you like e.g Blog. The same applies to <name> which is the name of your virtual environment.

The activated environment only works for the context it was activated in. For instance, here the activated environment only applies to <projectname> or <name> and nowhere else. The environment can also be deactivated by running the command deactivate and removed entirely by deleting its directory.

The virtual environment also provides an easier method to keep track of all packages installed in a project. With the activated environment, you can run the command pip freeze to list all installed packages.

You might also be wondering about virtual environment in other programming languages, you'd find some answers here.

Have you ever used virtual environments in Python? Do share your thoughts in the comment section. 😀

Resources:

code.tutsplus.com/tutorials/understanding-v..

realpython.com/python-virtual-environments-..

Comments (2)

Darsh Shukla's photo

Hi, nice article. I generally use pipenv or poetry and I found an awesome wrapper around virtualenv to manage different virtualenvs.

Joy Nwaiwu's photo

This is great too, thank you.