Understanding Project or Code Environments Through Python Programs

So, I’m currently learning to use Streamlit to visualize a data science project more easily. Then, on the documentation on the Streamlit website, there is a 30-day challenge named “30 Days of Streamlit.”

On the first day, I was asked to install Conda through Miniconda and do some setup for the environment.

There are two wordings that I haven’t fully understood, namely Conda and Environment. I’ve heard of Anaconda before, but not Conda. I’m also familiar with Environment, but I want to know more about why we need to use Conda and manage environments for a project. In this case, the context is a Python and Streamlit project.

Streamlit

Streamlit is an open-source app framework for creating and sharing data applications using Python. Streamlit allows you to build web apps for data science and machine learning projects with minimal effort.

On Day 1 page, there are three things that I haven’t fully understood why we have to do them. As for the rest, I already understand.

So, I tried to find out and got some insights that seem like they would be helpful if you experience them. Lets get started.

Understanding the Term ‘Environment’: Why Do We Need to Manage Python Environments?

Managing your Python environment is important for several reasons:

  1. Dependency Management:
    • Different projects often require different versions of the same packages. Managing environments allows you to isolate dependencies, ensuring that updates or changes in one project do not affect another.
    • For example, one project might require pandas version 1.2.0 while another might need version 1.1.5. Without environment management, these dependencies could conflict.
  2. Reproducibility:
    • Environments help ensure that your code runs consistently across different systems. By managing dependencies explicitly, you can share your environment configuration with others (using tools like requirements.txt or environment.yml), enabling them to recreate the exact setup you used.
    • This is crucial in research and production, where consistency and reproducibility of results are paramount.
  3. Isolation:
    • Virtual environments isolate project-specific dependencies from the system-wide Python installation. This prevents system-wide package changes from breaking your projects.
    • Isolation also means you can experiment with different versions of packages without affecting other projects.
  4. Security:
    • Using environments can mitigate security risks by limiting the scope of installed packages and ensuring that only the necessary dependencies for a project are included.
    • It also helps in quickly applying security patches to specific environments without affecting the whole system.
  5. Compatibility:
    • Different projects might require different versions of Python itself. Environments allow you to manage multiple versions of Python on the same system.
    • For instance, one project may be using Python 3.6 while another uses Python 3.8. Using environments, you can switch between these versions seamlessly.
  6. Ease of Collaboration:
    • Sharing a consistent environment setup helps collaborators work with the same configurations and dependencies, reducing the “it works on my machine” problem.
    • Tools like Conda environments and Docker containers provide a standardized way to share and deploy applications.

Tools for Managing Python Environments:

  1. venv:
    • A built-in module in Python 3. It provides basic support for creating lightweight virtual environments.
  2. virtualenv:
    • An external tool that offers more features than venv and works with Python 2 and 3.
  3. Conda:
    • A package, dependency, and environment management tool. It supports multiple languages, not just Python, and is particularly popular in data science and machine learning communities.
  4. Pipenv:
    • Integrates pip and virtualenv into a single tool for managing dependencies and virtual environments.
  5. Poetry:
    • A tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and manages (install/update) them for you.

By managing your Python environments effectively, you ensure that your development process is more organized, your projects are more reliable, and your collaborations are smoother.

So from the explanation above, we now know what the relationship between Conda and Environment is!

Does Setting Up Conda Create a Folder?

Yes, when you create a new Conda environment, Conda creates a directory for that environment. This directory contains the Python interpreter, libraries, and scripts necessary for the environment to function. Here’s a detailed look at where and how these directories are created:

Conda Environment Directory

  1. Location:

    • By default, Conda environments are created in the envs directory within your Conda installation directory. For example, if Conda is installed in ~/miniconda3, the environments will be located in ~/miniconda3/envs/.
  2. Creating a Conda Environment:

    • When you create a new environment using the command:
      conda create --name myenv python=3.8
      Conda creates a directory structure for myenv inside the envs directory.
  3. Custom Location:

    • You can also specify a custom location for your environment using the --prefix option. For example:
      conda create --prefix /path/to/myenv python=3.8

      This command creates the environment in the specified path instead of the default envs directory.

Example of Conda Environment Directory Structure

When you create a Conda environment named myenv, the structure looks something like this (assuming the default location):

~/miniconda3/envs/myenv/
├── bin/
│   ├── activate
│   ├── python
│   └── ...
├── lib/
│   ├── python3.8/
│   └── ...
├── include/
├── share/
└── ...
  • bin/: Contains executables for the environment, including the Python interpreter.
  • lib/: Contains the Python standard library and site-packages.
  • include/: Contains C headers for Python packages.
  • share/: May contain shared data for some packages.

How to Locate Your Conda Environments

  1. Listing All Environments:

    • You can list all Conda environments on your system using:
      conda env list
      This command shows the names and paths of all environments.
  2. Environment Details:

    • To see the details of a specific environment, including its location, you can use:
      conda info --envs

Activating and Using the Environment

  • Activating the Environment:

    conda activate myenv

    This modifies your PATH to point to the environment’s bin directory, making its executables available.

  • Deactivating the Environment:

    conda deactivate

By managing environments in this way, Conda ensures that each environment is isolated, preventing conflicts between dependencies of different projects. This isolation is particularly useful for maintaining reproducibility and consistency across different development and deployment stages.

Examples: Creating 2 Python Projects Using Different Environments

Let’s assume we have two projects: project1 and project2. We’ll create environments for each project, install specific dependencies, and run some sample code.

Step-by-Step Example

Step 1: Create Conda Environments

  1. Create Environment for Project 1:

    conda create --name project1-env python=3.8

    Activate the environment:

    conda activate project1-env

    Install dependencies (for example, numpy and pandas):

    conda install numpy pandas
  2. Create Environment for Project 2:

    conda create --name project2-env python=3.9

    Activate the environment:

    conda activate project2-env

    Install dependencies (for example, scipy and matplotlib):

    conda install scipy matplotlib

Step 2: Set Up Project Directories

Create directories for your projects:

mkdir project1
mkdir project2

Step 3: Write Sample Code for Each Project

  1. Project 1 Code (project1/main.py):

    import numpy as np
    import pandas as pd

    print("Project 1 Environment")
    array = np.array([1, 2, 3, 4, 5])
    df = pd.DataFrame(array, columns=['Numbers'])
    print(df)
  2. Project 2 Code (project2/main.py):

    import scipy
    import matplotlib.pyplot as plt
    import numpy as np

    print("Project 2 Environment")
    x = np.linspace(0, 10, 100)
    y = np.sin(x)

    plt.plot(x, y)
    plt.title("Sine Wave")
    plt.show()

Step 4: Run the Code in Each Environment

  1. Run Project 1 Code:

    • Activate the environment:
      conda activate project1-env
    • Navigate to the project directory and run the script:
      cd project1
      python main.py
    • Output:
      Project 1 Environment
      Numbers
      0 1
      1 2
      2 3
      3 4
      4 5
  2. Run Project 2 Code:

    • Activate the environment:
      conda activate project2-env
    • Navigate to the project directory and run the script:
      cd project2
      python main.py
    • Output:
      Project 2 Environment
      (A plot window showing a sine wave will appear.)

Summary

  • project1 uses project1-env with Python 3.8 and dependencies numpy and pandas.
  • project2 uses project2-env with Python 3.9 and dependencies scipy and matplotlib.

By using separate environments, you ensure that each project has its own dependencies and Python version, preventing conflicts and ensuring reproducibility.

Do we need to choose an environment before executing the project?

Yes, that’s correct. Before executing a project, you typically activate the environment associated with that project to ensure that the correct dependencies are available.

When you activate an environment, the system’s PATH variable is modified to point to the environment’s binaries and scripts. This means that when you run Python scripts or execute commands, the system uses the Python interpreter and packages installed within the activated environment.

So, before running a project, you should activate the corresponding environment. Here’s a summary of the steps:

  1. Activate Environment:

    • Navigate to the project directory.
    • Activate the environment associated with that project.
    • For example:
      cd path/to/project1
      conda activate project1-env
  2. Execute Project:

    • Run the Python script or execute commands related to the project.
    • For example:
      python main.py
  3. Deactivate Environment (optional):

    • After you’re finished with the project, you can deactivate the environment if you no longer need it.
    • For example:
      conda deactivate

By following this approach, you ensure that each project is executed within its own isolated environment, preventing conflicts and maintaining reproducibility.

Leave a Reply