How to Create Virtual Environments with Python [Tutorial]
a short hands-on introduction
Virtual environments are a tool used to separate different python environments, on the same computer. Traditionally, each computer has one installation of the python programming language with its respective set of modules> Those modules as well had one running version. This set-up is not flexible enough for projects that require specific versions of some of those modules or even of python itself. Therefore, we can make use of Virtual Environments to save an environments’ configuration. This way, we can make sure that our projects run on other systems identically as they did on our computers on which we developed them.
Thus, using virtual environments is a great way of making sure that your project runs identically on some other computer than the one you worked on. There are other advantages as well: you can try out your project on multiple python versions and different configurations. For example, if you use certain modules on your project, you wouldn’t want a sneaky versioni update to break parts of your application, would you?
Traditionally, developers would use virtual machines… but that solution is quite costly and time-consuming. Nowadays, a faster and less costly (in computer resources) option for python-based projects is to use a tool like venv.
…Which stands for Virtual ENV ironment.
To install venv we will use pip, which is a package manager for python. It is used to install python plugins basically and keep them up-to-date. It’s very easy to use: you just have to give it the name of the plugin (called module) you need.
Short Developer Tutorial
In this short practical introduction to venv, we will go together through the following steps:
install the venv modulecreate a virtual environmentactivate & deactivate the virtual environmentcustomise itsave its configurationcreate a second virtual environment & import configurations
So, to start using venv, open your terminal. (I use cmder on a windows machine).
The requirements is to have python installed and its package manager pip as well. If you’re not sure if you have them installed type into your terminal
Next step is to install venv. The pip module is actually named “virtualenv”, so the console command you need to use is “pip install virtualenv”:
If you get any errors, make sure that pip is updated. Run this command:
pip install --upgrade pip
…and then try again !
OK, now that we have venv set up on our python environment, let’s go ahead and spin up our first actual python virtual environment.
The command to run is:
where we named our first environment “environment-1”. You can, of course, name it whatever you want.
if you type “ ls “ you will see that a new folder has been created.
The next step is to activate the environment that we have just created. to do that, cd into the the “environment-1” folder. Inside there, we have a “Scripts” folder which contains some handy scripts. One of those scripts is “activate” which we will run like in the following screenshot:
The prompt that mentions the active environment, informs us what environment our machine is currently running.
to deactivate it, simply run this command:
What we have here:
and you can check which modules you have installed by running:
Let’s install a few modules:
pip install numpy pytz psutil
now let’s check them in our module list:
As we can see, we are have customised our environment-1 python instance.
Exporting/Importing a configuration
while the environment is active, we can save its configuration in an external file, which we can later import.
To generate the config file type this command:
pip freeze -local > requirements.txt
now, if we ls we can see that our config file has been generated:
We can even check the contents of the file (using the cat command) to verify it:
It contains, indeed, our modules that we previously installed.
Let’s create a new virtual environment:
now we have to activate it:
and finally, we can import the configuration file:
pip install -r requirements.txt
If we check our installed modules using “pip list” we can see that our second environment is now identical to the previous one.
So today, we learned how to use virtual environments: how to create them, activate & deactivate them, export & import their configurations.
The virtual environments are meant to be separated bundles of your packages (and their versions) that you use in your projects. When activated, the environment affects the system globally.
i.e. You don’t need to build your projects within the virtual environments folders. Your project can be placed anywhere on your computer.
Bonus Cheat Sheet
use this in the future:
Subscribe to get your daily round-up of top tech stories!