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

    bash
    conda --version
  2. Update Conda

    bash
    conda update conda
  3. Search for a Package

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

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

    bash
    conda remove <package-name>

Managing Conda Environments

  1. Create a New Environment

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

    Example:

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

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

    bash
    conda deactivate
  4. List All Environments

    bash
    conda env list
  5. Remove an Environment

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

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

    bash
    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

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

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

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

    bash
    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

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

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

    bash
    pip install -r requirements.txt
  4. Verify Installation

    bash
    pip list

Using Conda

  1. Navigate to Your Repo Directory

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

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

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

    bash
    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! 🚀