Using Python's Built-in venv Module

Python’s built-in venv module allows you to create lightweight virtual environments without needing external tools like virtualenv. It’s available in Python 3.3 and newer.


What is venv?

  • Isolated Environment: Each virtual environment is independent and contains its own Python interpreter and libraries.
  • Built-in Module: No additional installation is required.

Creating a Virtual Environment

Use the python command with the -m venv option to create a virtual environment.

Syntax

python -m venv env_name

Examples

  1. Create a virtual environment named venv:
python -m venv venv
  1. Specify a Python interpreter:
python3.11 -m venv myenv

This ensures the virtual environment uses a specific Python version.


Activating the Virtual Environment

To start using the virtual environment, you need to activate it.

Windows

venv\Scripts\activate

macOS/Linux

source venv/bin/activate

Once activated, the terminal prompt will display the environment name, e.g., (venv).


Deactivating the Virtual Environment

To stop using the environment, deactivate it:

deactivate

This returns you to the system’s global Python environment.


Managing the Environment

Check Installed Packages

To see the installed packages in your virtual environment:

pip list

Install Dependencies

Install packages using pip:

pip install package_name

Save Dependencies

To save all installed packages to a file:

pip freeze > requirements.txt

Reinstall Dependencies

Recreate an environment using the requirements.txt file:

pip install -r requirements.txt

Deleting the Virtual Environment

To delete a virtual environment, simply remove its directory:

rm -rf venv

Best Practices with venv

  1. Environment Naming:

    • Use descriptive names for your virtual environments, such as my_project_env.
  2. Project-Specific Environments:

    • Create the virtual environment within the project folder:

      cd my_project
      python -m venv venv
      
    • Add venv to your .gitignore file to avoid committing it to version control.

  3. Keep Dependencies Up-to-Date:

    • Regularly update packages:

      pip install --upgrade pip
      pip list --outdated
      pip install --upgrade package_name
      
  4. Use requirements.txt for Reproducibility:

    • Always maintain a requirements.txt file for team collaboration or deployment.
Task

Practice: Working with Python's Built-In venv

Objective: Learn to create, manage, and work with virtual environments using Python's built-in venv module.

  1. Create a Virtual Environment:

    • Create a virtual environment named test_env in the current directory.
    python -m venv test_env
    
  2. Activate and Install Packages:

    • Activate the virtual environment and install the requests library.

    On Windows:

    test_env\Scripts\activate
    

    On macOS/Linux:

    source test_env/bin/activate
    

    Then, install the requests library:

    pip install requests
    
  3. Save Dependencies:

    • Freeze the installed packages into a requirements.txt file.
    pip freeze > requirements.txt
    
  4. Recreate Environment:

    • Deactivate and delete the virtual environment.
    deactivate
    rm -rf test_env  # On macOS/Linux
    rmdir /S /Q test_env  # On Windows
    
    • Create a new virtual environment and reinstall dependencies from the requirements.txt file.
    python -m venv test_env
    source test_env/bin/activate  # On Windows: test_env\Scripts\activate
    pip install -r requirements.txt
    
  5. Experiment with Python Versions:

    • Create a virtual environment using a specific Python version if multiple versions are installed.
    python3.9 -m venv py39_env
    source py39_env/bin/activate  # On Windows: py39_env\Scripts\activate
    python --version
    

Copyright © 2025 Devship. All rights reserved.

Made by imParth