Handling Virtual Environments

An introduction to setting up a virtual environment in Python.

Why should we care?

For every new data science project that you start - be it in Python, R or in any other programming language - it is recommended that you create a virtual environment first, instead of running every new project via your global environment. Why? Because once you start to update some libraries - for example when dealing with a current project - then there is a risk that your older projects might break, because the newly updated library in your current project may have some dependency issues with another library that you were using in one of your old projects.

  • And the (worst case) consequence of this is: your entire (old, but still polished) project might not run anymore and many hundreds of hours of work can be wasted!

So to avoid those dependency-nightmares across a single global environment, virtual environments were created to allow every project to be an independent piece of software on its own. Hence, when updating a library, it will happen on the project-level, and not on the global-level anymore (and we will no longer break old projects)! 🔥

Providing an Example for Context

The main goal of this post is to show you how to set up a virtual environment by using a concrete example.

In this case, I will show the whole set up by creating a new project (with a virtual environment), in which I will install the python-library python-pptx, to automate the process of creating a powerpoint (which may be a handy tool for you in the future…?).

Set-Up

Before you start:
Make sure that you have python installed on your machine!

This set-up is made for users that have VS-Code as their IDE. However, it should work for any other editor as well:

  1. First, let’s create a project-folder. If you do not have one, then create one (you can call it anything you want). I called this folder powerpoint-automation. This will be our so-called “working directory” for this project.
  2. Next, we need to move into our working directory. For this step, we need a terminal. You need to open your terminal and type in the path, where your project (that we created in the first step) is located: cd /Users/jomaye/Documents/Programming/Python/projects/powerpoint-automation, where cd is the command to tell the computer to “move into…” and /Users/jomaye/Documents/Programming/Python/projects/powerpoint-automation is the path to my working directory.

Create a Virtual-Environment

Here’s how to create a virtual environment in Python (with the use of venv) in 3 steps:

  1. In your terminal, type in the following (make sure that you are inside your wd!): python3 -m venv ppp-env2. This will create the virtual-environment with the name ppp-env2.
  2. Next, you need to activate this environment:
    • If you use your Mac: source ppp-env2/bin/activate.
    • If you are on a Windows-Computer: ppp-env2\Scripts\activate.bat
  3. To verify if everything is working, simply type the following into your terminal: python --version

That’s it for the installation of the virtual environment! Congratulations on making it this far 🥳

Install the Packages needed

Now that the virtual environment is set up, we can start installing the packages that we need.

In this example, I simply need 1 package:

  • pip install python-pptx

Create your first automated Powerpoint

Now that you have installed the package:

  1. Create a new python-file inside your powerpoint-automation-folder.
    • Call it: hello-world.py
  2. Next, just copy-paste the following code inside your new hello-world.py-file:
from pptx import Presentation

prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"

prs.save('test.pptx')
  1. Now, run the Python-Script hello-world.py by typing in the following within the terminal: python hello-world.py.
    • If you run into an error like "File "hello-world.py", line 1, in <module> from pptx import Presentation occurs: Simply run python3 hello-world.py.

Potential Issue with Virtual Environments

For beginners, a very frustrating issue can happen:

If we change the name of any folder that we defined on the path a project with a virtual environment, it will cause our project to run the wrong Python version.

For example, when I changed my project-path, it caused my project to run on version 2.7.16 (instead of Python 3.8.2) and messed everything up! In order to understand why our project now runs on the wrong Python version, we need to go navigate to the following folder (assuming that you are currently in your project’s working directory): path_to_your_env/bin/activate.

Next, simply open the activate-file. After opening it, you should check out the VIRTUAL_ENV variable there. In my case, I see the following:

VIRTUAL_ENV="/Users/jomaye/Documents/Programming/Python/projects/libraries-to-try/statsmodels/env-statmodels"

Now, I simply modified the path of the VIRTUAL_ENV variable to the following:

    VIRTUAL_ENV="/Users/jomaye/Documents/Programming/Python/projects/data-science/statsmodels/env-statmodels"

As you can see from the above example, the problem was that I had renamed the libraries-to-try-folder to data-science. Thus the path to venv was wrong and caused the computer to automatically switch to the default Python version 2.7.16 which is installed on my Mac (instead of the Python 3.8.2 version I actually wanted…).

To check if you were able to resolve the issue, simply type the follwoing into your terminal: python --version.

If you are now running a more recent version (instead of the old 2.7.16 version) - like, in my case Python 3.8.2 - then your virtual environment should be back on track! 🤩

Conclusion

In this article, I showed the relevance of using a virtual environment when starting a new project. Be it for smaller or larger projects, having a controlled (virtual) environment relative to one single global environment significantly reduces the risks of having one of your older project not run anymore due to some dependency issue.

However, virtual environments have their disadvantages too. Changing the path to a project with a virtual environment will cause it to run the global python version installed on your computer, instead of the version you chose within your virtual environment, which most certainly will cause your project to not run anymore either. To resolve this issue, be sure to adapt the path of your VIRTUAL_ENV-variable within the ``activate-file situated on the path_to_your_env/bin/activate`-path of your working directory.

My interests include Behavioral Economics, Web Development, Data Science and everything related to Entrepreneurship.