Home/Blog/Python Virtual Environment Guide | Setup & Management
Python

Python Virtual Environment Guide | Setup & Management

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

Python Virtual Environment Guide | Setup & Management

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

Frequently Asked Questions

Find answers to common questions

Setting up a Python virtual environment using VirtualENV involves a series of straightforward steps that ensure your project dependencies are isolated from the system Python installation. Here’s a detailed guide: 1. **Install VirtualENV**: First, ensure that you have Python and pip installed on your system. You can check this by running `python --version` and `pip --version`. Once confirmed, install VirtualENV using the command: ```bash pip install virtualenv ``` This command fetches the latest version of VirtualENV and installs it. 2. **Create a New Project Folder**: Organize your projects by creating a dedicated directory. For example: ```bash mkdir project1 cd project1 ``` 3. **Create a Virtual Environment**: Inside your project folder, create a virtual environment by executing: ```bash virtualenv env ``` This creates a new directory named `env`, which contains the Python interpreter and a local version of pip. 4. **Activate the Virtual Environment**: Before you can install packages, activate the virtual environment: - On Windows: ```bash . ext{env} ext{Scripts} ext{activate} ``` - On macOS/Linux: ```bash source env/bin/activate ``` After activation, your command prompt should reflect the environment name, indicating that you are now working within this isolated space. 5. **Install Dependencies**: Now, you can install project-specific dependencies using pip. For example: ```bash pip install requests ``` This installation is now contained within the `env` folder and will not affect other projects or the system-wide Python installation. 6. **Managing Dependencies**: To keep track of your project's dependencies, it’s a good practice to create a `requirements.txt` file. You can generate this file with: ```bash pip freeze > requirements.txt ``` This file can be used later to replicate the environment elsewhere by running: ```bash pip install -r requirements.txt ``` 7. **Deactivating the Environment**: Once you finish your work, you can deactivate the virtual environment by running: ```bash deactivate ``` This returns your command line prompt to the system Python environment. **Challenges and Solutions**: - **Permission Issues**: If you encounter permission issues when trying to install packages, ensure you're running the command prompt with elevated privileges (especially on Windows). - **Compatibility Issues**: Regularly check for updates and compatibility of installed packages, as some libraries may have breaking changes in newer versions. **Best Practices**: - Always create a new virtual environment for each project to avoid dependency conflicts. - Regularly update your `requirements.txt` to reflect the current state of your environment. - Consider using version control (like Git) for your project folder, but ensure to add the `env` directory to your `.gitignore` file to avoid committing unnecessary files.

Automate Your IT Operations

Leverage automation to improve efficiency, reduce errors, and free up your team for strategic work.