Virtual Environment, how to create and use it

Guilherme Bordotti
3 min readMay 6, 2021

A tutorial with example and explanations

Photo by Tamara Malaniy on Unsplash

If you are here I assume that you already know what it is you can skip the next paragraph, but if not I’ll explain with an example.

Problem to solve

Imagine that you are working on a project which uses a few libraries, you may want to deploy this project on a server local or cloud and for that you need to install the same libraries (quite easy isn’t it?).

After all libraries installed you will run your project and it may show some issues that are not found on your machine, how can this happen if you just copy and paste the script? Different versions of libraries with deprecated methods can easily break your project and the Virtual Enviroment is here to help you avoid this kind of headache. This tool will keep “separated” libraries for every environment that you create, and this may be one for project.

Setup

First you need to verify if your python installation is mapped on your system’s variables.

A simple way to do that is type “py — version” and press enter.

The output must be something like the image down below which shows the last version installed and mapped.

If the output shows that “py is not recognized as an internal function” I explain here how to setup python on your environment variables.

Time to install the tool that will help you, just type “pip install virtualenv”.

After installation you can navigate to your project folder (using your terminal/cmd/power shell depending on your system) and there you will write “virtualenv myenv” and press enter.

The last step will create a copy of your python installation without any third-party library, looking like the first time you are installing python.

Usage

Let’s use the virtual environment created, on your folder will have a new folder named “myenv”, you can navigate into the folder and the subfolder named “Scripts” or just write “myenv/Scripts/activate” and press enter.

This should activate your virtual environment and now you can install all dependencies for your project.

Now your virtual env is ready to use, and every time you run your script you should activate this in order to use the controlled dependencies.

Steps to activate virtualenv

But you may think, how will this solve the example problem? It’s easy, with your virtual env activated just type “pip freeze > requirements.txt”, this command will create a file with all your libraries installed AND the version.

Exporting libraries installed

With the file created just copy to your server or the place you want to deploy, with the terminal navigate to your project folder, activate the virtual environment learned here and then type “pip install -r requirements.txt”.

This last step will read the requirements file and install all libraries listed there and the version specified.

Requirements file

Now you’re ready to use and keep dependencies separated from each project, this tool has a lot of options to be used and can be consulted on docs down below.

Want to deactivate and create a new virtual env? Just type “myenv/Scripts/deactivate” and go for it!

Virtualenv documentation: https://virtualenv.pypa.io/en/stable/

Youtube guide: Coming soon..

--

--

Guilherme Bordotti

Love for my family and motorcicles, automations developer with 3+ yrs exp with Python.