Skip to content

Python Versions and Virtual Environments

In Python development, managing different project dependencies and Python versions is a common challenge. One project might need a specific version of a library, while another project might need a different incompatible version of the same library. Virtual environments were born to solve this problem.

Python Version Management

Sometimes, you may need to use multiple Python versions on the same computer (e.g., using Python 3.8 and Python 3.10 simultaneously). There are many tools for managing multiple Python versions; one of the most popular is pyenv.

  • pyenv: Is a popular command-line tool that allows you to easily install, switch, and manage multiple Python versions. It's widely used on macOS and Linux. For Windows users, you can use pyenv-win.

Why do we need multi-version management?

  • Compatibility Testing: Ensure your code works correctly under different Python versions.
  • Maintaining Legacy Projects: Some older projects might depend on Python versions that are no longer the latest.

What is a Virtual Environment?

A virtual environment is a self-contained directory tree that contains a specific version of Python installation plus some additional packages. The core idea is to create an isolated, independent Python environment for each project.

Benefits of Using Virtual Environments:

  1. Dependency Isolation: Each project has its own independent libraries and dependencies, avoiding version conflicts between different projects. You can install requests==2.20.0 for one project while simultaneously installing requests==2.25.1 for another project.
  2. Keep Global Environment Clean: Avoid installing all packages into the system's global Python environment, which can make the global environment messy and difficult to manage.
  3. Explicit Project Dependencies: When you set up a virtual environment for a project, all packages in that environment are needed by that project. This makes it very easy to generate a requirements.txt file, facilitating collaboration with others.

How to Use Virtual Environments?

Starting from Python 3.3, the standard library includes the venv module for creating virtual environments. This is currently the recommended standard practice.

1. Creating a Virtual Environment

Open your terminal or command line, navigate to your project directory, and run the following command:

bash
# python3 -m venv <name_of_virtual_environment>
# Usually, we name the virtual environment 'venv' or '.venv'
python3 -m venv venv

After execution, you'll see a new folder named venv under your project directory. This folder contains a copy of the Python interpreter and some management scripts.

Tip: It's best to add the virtual environment folder (such as venv/) to your .gitignore file so it won't be committed to version control.

2. Activating the Virtual Environment

After creating an environment, you need to activate it to start using it.

  • On macOS / Linux:

    bash
    source venv/bin/activate
  • On Windows (using Command Prompt):

    bash
    .\venv\Scripts\activate
  • On Windows (using PowerShell):

    powershell
    .\venv\Scripts\Activate.ps1

After activation, you'll see the text (venv) appear before your command line prompt, indicating that you are now in a virtual environment. After this, the python and pip commands you use will be from this independent environment.

3. Installing Packages in a Virtual Environment

After activating the environment, you can use pip to install packages as usual, but these packages will only be installed into the current virtual environment.

bash
(venv) $ pip install requests
(venv) $ pip install numpy pandas

4. Deactivating the Virtual Environment

When you've finished your work and want to return to the global Python environment, simply run:

bash
(venv) $ deactivate

Managing Project Dependencies: requirements.txt

To make it easy for other developers (or future you) to rebuild the project's environment, you should create a requirements.txt file to record all dependencies.

  • Generating requirements.txt: After activating the virtual environment, run the following command. It will output all installed packages in the current environment along with their version numbers to the file.

    bash
    pip freeze > requirements.txt
  • Installing Dependencies from requirements.txt: When others obtain your project, they can create and activate their own virtual environment, then run the following command to install all required packages:

    bash
    pip install -r requirements.txt

Content is for learning and research only.