Python virtual environments & libraries

Origin Audio
5 min readFeb 1, 2022

One of the most important parts of python development is virtual environments and libraries. You may ask: what are virtual environments exactly and why do we need them? This post will explain to you what libraries are, why we use and how you could utilize them for your own projects.

Virtual Environments

Virtual environments are one of the most important parts of python development. Simplified, a virtual environment is a folder on your computer, that includes everything that is required for a basic python application to work.

Every project you start should have its own virtual environment. Why you might ask? This has something to do with libraries. Every library has different versions available and every application has its own unique requirements. Imagine you would install every version required into your global python directory, each version would create new conflicts, due to different dependencies. However, if you use virtual environments each application can install the version it requires without these conflicts occurring.

For example: If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict, and installing either version 1.0 or 2.0 will leave one application unable to run.

Ok, great — how do I get started now?

Creating a new virtual environment is simple. Python contains the venv library. To create a new virtual environment, open a terminal /PowerShell of your choice and use the command:

python3 -m venv tutorial-env

Let’s check what this command actually does:

  • python3: with python3 the correct python interpreter is being called. A lot of systems have both python version 2 and python version 3 installed. To target python version 3 “python3” is being used. If the command does not work on your system try “python”.
  • -m venv: informs python about your intention to create a virtual environment
  • tutorial-env: the folder created by this command will have this name. But you can name it otherwise if desired, as long as you follow the guidelines of your OS.

Another code-example would be:

python3 -m venv venv

This will create a new directory with the name venv.

Using the virtual environment

However, just creating a virtual environment is not enough. To make use of the virtual environment one has to activate it. How this is done, depends on the OS you are using.

macOS:

  1. Make sure to be in the root directory of your application
  2. Execute the following line of code:

source venv/bin/activate

3. The activated virtual environment will be indicated by a (venv) in front of each new line in the terminal

Windows:

  1. Make sure to be in the root directory of your application
  2. Execute the following line of code:

.\venv\Scripts\activate

3. The activated virtual environment will be indicated by a (venv) in front of each new line in the terminal. As can be seen below.

After successfully activating the virtual environment, it is ready to be used and new libraries can be installed. Here you can see a summary of the commands above combined. Please note the green venv, indicating that the virtual environment is activated.

Libraries

A library is a collection of python files providing different functionalities. For example, the pathlib library provides functionality for interacting with the filesystem. Standard libraries like the aforementioned pathlib are shipped with each python installation and third-party libraries, maintained by other developers, can be installed when needed. An application can consist of multiple libraries.

Installing libraries

To install a library, use the “pip install” command followed by the package name. Pip is the standard package manager and a good explanation about what pip is can be found here. One can search for libraries by using the python package index which can be found here. Just enter the name of the library and the indexer will show you a list of search results.

Important: Make sure to select the correct library, as there might be multiple libraries with similar names. Also, some of them might be attempts to inject malicious code into your application!

As an example, we will use the pandas library. A powerful library that makes it easy to work with relational and labeled data. To install pandas we use:

pip install pandas

This will trigger the install procedure and after successful installation, the library is available for use in your application. To use the library in your Python application, just do the following:

import pandas as pd

Make sure to check out the documentation of the library you want to use, as they usually provide quick start tutorials and explanations on how to use and import the library.

If you are still facing issues setting up virtual environments or want to know more about how to install libraries, feel free to join us for free help. Origin Audio Discord

--

--