Getting Started with Python Environments: Conda, venv, and Installing Dependencies

Managing Python projects efficiently requires a solid understanding of virtual environments and dependency management. This guide introduces you to two popular tools—conda and venv—and shows you how to set up a project and install dependencies from a requirements.txt file.


Why Use Virtual Environments?

Virtual environments allow you to:

  • Isolate dependencies for each project.
  • Avoid conflicts between package versions.
  • Maintain a clean global Python installation.

Both conda and Python’s built-in venv module are excellent tools for creating virtual environments.


Getting Started with Conda

What is Conda?

Conda is a powerful package, dependency, and environment manager for Python and other languages. It’s available through Anaconda (a comprehensive suite) or Miniconda (lightweight).

Basic Conda Commands

  1. Check if Conda is Installed

    conda --version
    
  2. Update Conda

    conda update conda
    
  3. Search for a Package

    conda search <package-name>
    
  4. Install a Package

    conda install <package-name>
    
  5. Remove a Package

    conda remove <package-name>
    

Managing Conda Environments

  1. Create a New Environment

    conda create --name <env-name> python=<version>
    

    Example:

    conda create --name myenv python=3.9
    
  2. Activate the Environment

    conda activate <env-name>
    
  3. Deactivate the Environment

    conda deactivate
    
  4. List All Environments

    conda env list
    
  5. Remove an Environment

    conda env remove --name <env-name>
    
  6. Export an Environment

    conda env export > environment.yml
    
  7. Recreate an Environment

    conda env create -f environment.yml
    

Getting Started with venv

The venv module is lightweight and comes pre-installed with Python (since Python 3.3).

Basic venv Commands

  1. Create a Virtual Environment

    python -m venv <env-name>
    
  2. Activate the Virtual Environment

    • Linux/Mac:
      source <env-name>/bin/activate
      
    • Windows:
      <env-name>\Scriptsctivate
      
  3. Deactivate the Virtual Environment

    deactivate
    
  4. Delete a Virtual Environment Simply remove the directory:

    rm -rf <env-name>  # Linux/Mac
    rmdir /S <env-name>  # Windows
    

Installing Packages from requirements.txt

When you clone a new repository, it often includes a requirements.txt file listing the dependencies. Here’s how to install them.

Using venv

  1. Navigate to Your Repo Directory

    cd <path-to-your-repo>
    
  2. Create and Activate a Virtual Environment

    python -m venv venv
    source venv/bin/activate  # Linux/Mac
    venv\Scriptsctivate     # Windows
    
  3. Install Dependencies

    pip install -r requirements.txt
    
  4. Verify Installation

    pip list
    

Using Conda

  1. Navigate to Your Repo Directory

    cd <path-to-your-repo>
    
  2. Create and Activate a Conda Environment

    conda create --name <env-name> python=<version>
    conda activate <env-name>
    
  3. Install Dependencies

    • Using Conda:
      conda install --file requirements.txt
      
    • Or using Pip (if requirements.txt lists PyPI packages):
      pip install -r requirements.txt
      
  4. Verify Installation

    pip list
    

Conda vs. venv: Which Should You Use?

FeatureCondavenv
Supported languagesPython, R, Ruby, JavaScript, etc.Python only
Package managementYesNo
Environment isolationYesYes
Cross-platformYesYes

Recommendations

  • Use Conda if you need:

    • Multi-language support.
    • Built-in package management.
    • Precompiled scientific libraries.
  • Use venv if you:

    • Only need Python.
    • Want a lightweight, simple solution.

Conclusion

Understanding and using virtual environments effectively can significantly improve your Python development workflow. Whether you choose Conda for its powerful features or venv for its simplicity, these tools will help you manage your projects and dependencies like a pro.

Happy coding! 🚀