Python: How to do environments

Explanation of envirnonments in Conda and PIP

Posted on: 21 Oct 2023

Introduction

People commonly use Conda and Pip to manage their development environments and dependencies.

Conda

Conda environments in Python are isolated workspaces that allow you to manage project-specific dependencies and isolate packages from your system-wide Python installation. Conda is a package manager and environment manager that provides a powerful way to create, manage, and switch between these environments. Here are the basics of Conda environments in Python:

  1. Creating a Conda Environment:

    You can create a Conda environment using the conda create command. For example, to create a new environment named “myenv” with a specific Python version (e.g., Python 3.8), you can run:

    conda create --name myenv python=3.8
    

    This will create an isolated environment with the name “myenv” and install Python 3.8 within it.

    Or you can already have a file telling all the packages in environment.yml

name: mycondaenv  # Name of the Conda environment
channels:       # Specify Conda channels for package sources
  - defaults
  - conda-forge

dependencies:   # List of package requirements
  - python=3.9  # Specify the Python version
  - numpy=1.21.1
  - pandas=1.3.1
  - scikit-learn=0.24.2
  - jupyter=1.0.0
  - matplotlib
  - requests
conda env create -f environment.yml
  1. Activating an Environment:

    To use an environment, you need to activate it. The activation command depends on your operating system:

    • On Windows:

      conda activate myenv
      

    When an environment is activated, your command prompt or terminal should indicate the environment name, showing that you are working within that environment.

  2. Using Packages:

    You can use Conda to install packages into the active environment. For example, you can install the Pandas library:

    conda install pandas
    

    This installs the Pandas library within the active environment. Packages installed in one environment do not affect other environments or your system-wide Python installation.

  3. Listing Installed Packages:

    You can list the packages installed in the active environment using the following command:

    conda list
    

    This provides a list of installed packages and their versions.

  4. Deactivating an Environment:

    To exit an environment and return to the base (system-wide) environment, you can deactivate it with the following command:

    conda deactivate
    
  5. Exporting and Creating Environment Files:

    You can export the environment’s configuration to a file using the conda list --export command. For example:

    conda list --export > environment.yml
    

    This creates an environment.yml file, which you can share with others or use to recreate the environment.

  6. Creating an Environment from a File:

    You can create a new environment from an environment file using the conda env create command. For example:

    conda env create -f environment.yml
    

    This command creates a new environment based on the package specifications in the environment.yml file.

Conda environments provide a powerful way to manage dependencies and create isolated development environments for different projects. They are particularly useful for data science and scientific computing projects where different projects may require different versions of libraries and tools.

Delete the conda environments

You can delete Conda environments that you no longer need. Deleting environments can help free up disk space and keep your Conda environment list clean. Here’s how to delete a Conda environment:

  1. List Your Environments:

    First, list your Conda environments to see which ones you have. Open your command prompt or terminal and run:

    conda env list
    

    This will display a list of your Conda environments along with their paths.

  2. Deactivate the Environment (if activated):

    If the environment you want to delete is currently activated, make sure to deactivate it. You can do this by running:

    conda deactivate
    
  3. Delete the Environment:

    To delete a Conda environment, use the following command, replacing myenv with the name of the environment you want to delete:

    conda env remove --name myenv
    

    If the environment was created in a specific location, you can specify the full path to the environment instead of the name.

    Example for a specific path:

    conda env remove --prefix /path/to/environment
    

    Note: Deleting an environment is permanent and cannot be undone. Make sure you want to delete the environment before proceeding.

Once you’ve run the conda env remove command, the specified environment will be permanently deleted from your system.

Always be careful when deleting environments, especially if they contain important data or configurations. It’s a good practice to back up any critical data or settings before deleting an environment to avoid accidental data loss.

No Conda

You can create and activate Python virtual environments using venv or Conda. For example, to create a virtual environment using venv:

   python -m venv myenv

To activate the environment:

   .\myenv\Scripts\activate

Of course things will not work your way! You might get an error.

because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.

How to fix this?

To delete environments

deactivate the environment first

Then, Remove-Item -Path .\env -Recurse

Loading packages like environment.yml but with PIP

Here’s an example of a requirements.txt file with Python package requirements:

# This is a comment
# You can use comments to provide explanations or notes

# Basic package requirements
numpy>=1.18.5  # Specific version of the NumPy library
pandas>=1.0.1  # Minimum required version of Pandas

# You can use operators to specify version constraints:
# - == for an exact version
# - >= for a minimum version
# - <= for a maximum version

# Additional packages
requests>=2.22.0
matplotlib>=3.2.0
scikit-learn

In this example:

Once you have created or obtained a requirements.txt file, you can use it to install the specified packages or recreate a Python environment with the same package versions by running the following command:

pip install -r requirements.txt

This command will read the requirements.txt file and install the listed packages with their specified versions or constraints.

To create a Python virtual environment based on the requirements listed in a requirements.txt file, add additional packages to that environment using pip, and then save the updated requirements to a requirements.txt file, you can follow these steps:

To create a virtual environment based on these requirements, run the following command:

python -m venv myenv  # Create a new virtual environment
.\myenv\bin\activate  # Activate the virtual environment
pip install -r requirements.txt  # Install the initial package requirements

This creates a virtual environment named “myenv” and installs the packages listed in the requirements.txt file.

Once the virtual environment is activated, you can use pip to add more packages to it. For example, let’s add the requests and beautifulsoup4 packages:

   pip install requests beautifulsoup4

These packages will be installed into the active virtual environment.

To save the updated package requirements, run the following command while still in the virtual environment:

   pip freeze > updated_requirements.txt

This will create a new updated_requirements.txt file that includes all the packages, both the original ones from requirements.txt and the additional ones you installed.

Now, you have the updated_requirements.txt file that reflects the current state of the virtual environment, including the added packages. This file can be shared with others, used for documentation, or to recreate the environment exactly as you have it.

References

https://www.youtube.com/watch?v=1VVCd0eSkYc Master the basics of Conda environments in Python Great video to explain conda environment and how to do things