In Python development, have you ever faced the frustration of wanting to install Django 1.11 for Project A while also installing Django 2.2 for Project B? If you install them globally, Project A might run fine, but Project B could throw errors due to version conflicts. This is a classic example of “dependency chaos”—different projects requiring different versions of third-party libraries, where global installation causes overwriting, leading to broken project functionality.

What is a Virtual Environment?

A virtual environment acts like an “independent room” for each project, where each room has its own Python interpreter and dependencies, ensuring no interference between projects. This way, Django 1.11 in Project A won’t conflict with Django 2.2 in Project B, and the global Python environment remains clean and uncluttered.

Why Choose virtualenv?

virtualenv is one of the most popular virtual environment tools for Python. It is lightweight, open-source, and cross-platform. It simplifies creating, managing, and isolating Python environments, making it the “standard tool” to solve dependency conflicts.

Installing virtualenv

First, ensure Python and pip (Python’s package manager) are installed on your computer. Open your command line and check the Python version with:

python --version  # or python3 --version

If a version (e.g., Python 3.8.10) is displayed, Python is installed. Next, install virtualenv using pip:

pip install virtualenv  # For Python 3, use pip3 to avoid version confusion

Verify the installation with:

virtualenv --version  # Output like "virtualenv 20.16.6" indicates success

Creating a Virtual Environment

Assume your project folder is my_project. Navigate to the project directory:

cd my_project  # On Windows: cd C:\Users\YourName\my_project

Create a virtual environment (e.g., named venv; you can customize the name):

virtualenv venv  # Generates a virtual environment named 'venv'

After execution, a venv folder will appear in your project directory, containing a standalone Python interpreter and dependencies.

Activating the Virtual Environment

To use the virtual environment, you must activate it. Commands vary by operating system:

  • Windows (Command Prompt - CMD):
  venv\Scripts\activate.bat
  • Windows (PowerShell):
    First, set the execution policy (required only once):
  Set-ExecutionPolicy RemoteSigned -Scope CurrentUser  # Press 'Y' to confirm

Then activate:

  venv\Scripts\Activate.ps1
  • Mac/Linux:
  source venv/bin/activate

When activated, your command prompt will show (venv), indicating you’ve entered the virtual environment.

Installing Dependencies in the Virtual Environment

Once activated, libraries installed with pip will only exist within this environment and won’t affect the global Python installation. For example, install the requests library:

pip install requests  # requests is now installed only in the 'venv' environment

To list installed dependencies:

pip list  # Shows all dependencies in the current environment

Deactivating the Virtual Environment

To return to the global environment, run:

deactivate

The (venv) prefix will disappear, and you’ll return to the normal command line.

Deleting a Virtual Environment

Simply delete the venv folder in your project directory to completely remove the virtual environment.

Pro Tips

  • Specify Python Version: If you have multiple Python versions, specify the interpreter during creation:
  virtualenv --python=python3.8 venv  # Creates 'venv' using Python 3.8
  • Share Dependencies: Export dependencies to requirements.txt for easy collaboration:
  pip freeze > requirements.txt  # Exports current dependencies

Others can install the same dependencies with:

  pip install -r requirements.txt

Summary

Virtual environments are an essential “must-have” for Python development. They isolate dependencies between projects, preventing version conflicts. With virtualenv, you can easily create, activate, use, and delete isolated environments, making your development process more efficient and organized.

Xiaoye