Using Virtualenv for Virtual Environments

Python’s virtualenv is a tool to create isolated Python environments. It helps manage dependencies for different projects, ensuring no conflicts between libraries or versions.


What is virtualenv?

virtualenv allows you to create environments where:

  • Each environment has its own installation of Python.
  • Installed packages are isolated from other projects.

Installing virtualenv

To install virtualenv, use pip:

pip install virtualenv

To verify the installation:

virtualenv --version

Creating a Virtual Environment

Use virtualenv to create a new environment:

virtualenv env_name

Examples:

  1. Create a virtual environment in the current directory:
virtualenv myenv
  1. Specify a Python interpreter:
virtualenv -p python3.11 myenv

This allows you to use a specific Python version.


Activating a Virtual Environment

To use the environment, activate it.

Windows

myenv\Scripts\activate

macOS/Linux

source myenv/bin/activate

When activated, you’ll see the environment name in your terminal prompt:

(myenv) $

Deactivating a Virtual Environment

To stop using the environment, deactivate it:

deactivate

This will return you to the system Python environment.


Deleting a Virtual Environment

To remove a virtual environment, simply delete its directory:

rm -rf myenv

Using virtualenvwrapper (Optional)

For easier management of multiple virtual environments, you can use virtualenvwrapper.

Installation

Install virtualenvwrapper using pip:

pip install virtualenvwrapper

Configure virtualenvwrapper (Linux/macOS)

Add the following to your .bashrc or .zshrc:

export WORKON_HOME=$HOME/.virtualenvs
source $(which virtualenvwrapper.sh)

Reload your shell:

source ~/.bashrc

Commands:

  1. Create a new environment:
mkvirtualenv env_name
  1. Activate an environment:
workon env_name
  1. List all environments:
lsvirtualenv
  1. Remove an environment:
rmvirtualenv env_name

Best Practices with virtualenv

  1. Use requirements.txt:

    • Generate a list of installed packages:

      pip freeze > requirements.txt
      
    • Reinstall the dependencies in another environment:

      pip install -r requirements.txt
      
  2. Project-specific Environments:

    • Create a virtual environment within your project folder:

      cd my_project
      virtualenv venv
      
    • Add the environment directory (venv) to .gitignore to avoid committing it to version control.

  3. Environment Variables:

    • Store project-specific settings using environment variables.
    • Use .env files to keep sensitive information like API keys.
Task

Practice: Working with Virtualenv

Objective: Learn to create, manage, and utilize virtual environments using virtualenv and virtualenvwrapper.

  1. Install virtualenv:

    • Install the virtualenv package using pip and verify its installation.
    pip install virtualenv
    virtualenv --version
    
  2. Create a Virtual Environment:

    • Create a virtual environment named test_env.
    • Activate the environment and install the requests library.
    virtualenv test_env
    source test_env/bin/activate  # On Windows: test_env\Scripts\activate
    pip install requests
    
  3. Use requirements.txt:

    • Freeze the installed packages into a requirements.txt file.
    • Deactivate the current environment and create a new environment.
    • Use the requirements.txt file to reinstall packages in the new environment.
    pip freeze > requirements.txt
    deactivate
    virtualenv new_env
    source new_env/bin/activate
    pip install -r requirements.txt
    
  4. Experiment with Python Versions:

    • Create a virtual environment using a specific Python version (e.g., Python 3.9 if installed).
    • Activate the environment and verify the Python version.
    virtualenv -p /path/to/python3.9 py39_env
    source py39_env/bin/activate
    python --version
    
  5. Explore virtualenvwrapper:

    • Install and configure virtualenvwrapper.
    • Create, activate, and delete virtual environments using virtualenvwrapper commands.
    pip install virtualenvwrapper
    export WORKON_HOME=~/Envs
    mkdir -p $WORKON_HOME
    source /usr/local/bin/virtualenvwrapper.sh  # Adjust path as necessary
    
    mkvirtualenv wrapper_env
    workon wrapper_env
    rmvirtualenv wrapper_env
    
  6. Bonus: Understand Environment Isolation:

    • Test the isolation of environments by installing a package (e.g., flask) in one environment and verifying that it is not available in another.

Copyright © 2025 Devship. All rights reserved.

Made by imParth