Python Virtual Environment Guide

"Crystal ball reflecting a forest, symbolizing the concept of a Python virtual environment creating isolated spaces for development."

Python Virtual Environment Guide

Master isolated Python environments to prevent dependency conflicts and improve development workflow

By default Python only gives you a single environment for your whole system. This means when you install Python, or a package using Pip, you are installing that version of python or that version of the python package for the entire system. And all scripts you run will interact with that same version of python or package.

However, there are significant benefits to having a different python environment for each python application. Each application may have different dependencies and may have compatibility issues with future versions of python packages. Virtual environments solve these problems by creating isolated spaces for your Python projects.

Installing VirtualENV

VirtualENV is a python package that gives you the ability to create a new python environment in each folder. It is easy to install if you already have pip installed.

Once you have pip installed, you can install virtualenv by running:

pip install virtualenv

Or, you might have to run:

sudo pip install virtualenv

Windows Note: On Windows, you may need to run this from an elevated command prompt.

Creating Your First Virtual Environment

When using Python without virtualenv, everything is installed system wide. That is why you sometimes have to elevate your permissions to install or upgrade packages via pip. Virtualenv turns each folder into an isolated python environment.

Step-by-Step Setup

  1. Open a new command prompt or terminal
  2. Create a new folder called project1: mkdir project1
  3. Enter that folder: cd project1
  4. Create the virtual environment:
virtualenv env

This will create the new virtual environment for your project. If you type ls or dir (depending on OS), you will see that there is now a folder called env. This folder contains all of the binaries for your new environment.

Important Note: If you are including this folder in source control, it would be good to add the env folder to your .gitignore file.

Using Your Virtual Environment

Let’s test your new virtual environment to see how it isolates dependencies. Navigate into your project1 folder, and create a file called test1.py. Within test1.py, add:

import requests

Save your file, then go back to your terminal/command prompt, navigate to your project1 folder, and run:

env/bin/python test1.py

This command is using the copy of python in your newly created environment to run your test python script. You should get an error because in your newly created environment, you don’t have any modules installed.

Installing Packages in Your Environment

You can install the requests module by typing:

env/bin/pip install requests

After installing requests, run your test1 script again:

env/bin/python test1.py

You shouldn’t get any more errors. This demonstrates that what you do in project1 is independent of what you do in other projects.

Activating and Deactivating Environments

Up to this point we have typed env/bin in front of every command. This gives you flexibility, but most of the time you are going to want to run a bunch of commands within a given environment. Typing env/bin in front of every command will get old very quickly.

Activating Your Environment

The activate script makes changes to the way your path variable works and re-points all of your python and pip commands to the current virtual environment. To run the activate script, type:

source env/bin/activate

You will know it worked because the environment name will be shown at the beginning of the prompt:

(env) seans-macbook:git sean$

Deactivating Your Environment

When you are done working in that environment, you can get out of it by running the deactivate command:

deactivate

After you run the deactivate command, you will know it worked because your prompt will go back to normal:

seans-macbook:git sean$

Key Benefits of Virtual Environments

  • Dependency Isolation: Each project can have its own package versions
  • No Permission Issues: Install packages without administrator privileges
  • Clean Development: Avoid conflicts between different project requirements
  • Easy Testing: Test different package versions without affecting system Python

Elevate Your IT Efficiency with Expert Solutions

Transform Your Technology, Propel Your Business

Now you can create a new environment every time you have a new project and you won’t have to worry about one project’s dependencies stepping on your second project’s dependencies. At InventiveHQ, we combine industry expertise with innovative practices to enhance your development workflows and streamline your IT operations.