Virtualenv Setup

What Is a Virtual Environment?

A virtual environment is a tool in Python that creates isolated environments for projects, allowing different dependencies for each project. It helps avoid conflicts between packages and versions in the global Python installation.

Use Case:

A real-time use case for virtualenv is when working on multiple Python projects that require different versions of the same library.

Example: Imagine you are working on two projects:
  • Project A uses Django 4.1, which requires Python 3.10.
  • Project B uses Django 3.2 (LTS), which works best with Python 3.9.

Without virtualenv, installing one version of Django would conflict with the other. By using virtualenv, you can create separate environments for each project, ensuring that the correct version of Django is installed and running in each isolated environment, avoiding version conflicts.

Top 3 Ways to Create a Virtual Environment

  1. Using venv (Standard Library):
  2. The venv module creates lightweight, isolated environments with separate packages, independent of the global Python installation. It is a built-in Python module, so no additional installation is required.

    Creating a Virtual Environment with venv
    # Create the virtual environment
    python -m venv my_project_env
    
    # ---------------- This step is common for all environments ----------------
    
    # Activate the virtual environment:
    # On Windows:
    my_project_env\Scripts\activate
    
    # On macOS/Linux:
    source my_project_env/bin/activate
    
    # Install dependencies (e.g., Django)
    pip install django
    
    # Deactivate the virtual environment when done:
    deactivate
  3. Using virtualenv (Third-Party Package):
  4. Virtualenv is a third-party package that creates isolated Python environments to manage project dependencies separately. Since virtualenv is a third-party library, it needs to be installed separately.

    Creating a Virtual Environment with virtualenv
    # Step 1: Install virtualenv (if not already installed)
    pip install virtualenv
    
    # Step 2: Create the virtual environment
    virtualenv myenv
    
    # Step 3: Activate the virtual environment:
    # On Windows:
    myenv\Scripts\activate
    # On macOS/Linux:
    # source myenv/bin/activate
    
    # Step 4: Install dependencies (example: install Django)
    pip install django
    
    # Step 5: Deactivate the virtual environment when done
    deactivate
  5. Using conda (Anaconda):
  6. Conda manages both Python and non-Python dependencies, making it ideal for data science and machine learning projects. It provides pre-built binaries for easier and faster installation of complex packages. To use Conda, you must first install it and ensure it is added to your system's environment variables for easy access from the command line.

    Prerequisite: Make sure Conda is installed on your system and check using conda --version

    Creating a Virtual Environment with Conda
    # Step 1: Create a new Conda environment with Python 3.10
    conda create --name myenv python=3.10
    
    # Step 2: Activate the Conda environment:
    # On Windows/macOS/Linux:
    conda activate myenv
    
    # Step 3: Install dependencies (example: install Django)
    conda install django
    
    # Step 4: Deactivate the Conda environment when done
    conda deactivate

Exercise

  1. Fill in the Blanks:
    • - The __________ module is used to create isolated environments in Python.
    • - To activate a virtual environment in Windows, the command is __________.
    • - The virtualenv is a __________ package that must be installed using pip.
    • - Virtual environments help avoid __________ between different versions of libraries.
    • - The command to deactivate any virtual environment, regardless of the tool used, is __________.