Skip to content

Conda environments

dusankr edited this page May 29, 2023 · 4 revisions

Return to the main page

Table of content

External links (what is Conda-Miniconda-Anaconda, why and how to use them)

This description is based on following links and ChatGPT

Basic description

Conda is a powerful package management system that can be used to install and manage software packages, libraries, and dependencies across different platforms and programming languages. Conda environments provide a way to isolate software packages and dependencies in a self-contained environment, so that they do not interfere with other packages and dependencies on the same system. This is particularly useful when you need to work on multiple projects or develop software in different programming languages. By creating a separate environment for each project or programming language, you can ensure that the software packages and dependencies required for each project are installed and managed independently of each other.

Conda's environments also make it easy to manage and reproduce software environments across different platforms and operating systems. By exporting the environment specification to a YAML file, you can easily recreate the environment on a different system or share it with others. This is particularly useful when you need to collaborate with others or share your work with others who may not have the same software environment as you.

Finally, Conda environments provide a way to manage different versions of the same software package or library. This is particularly useful when you need to maintain compatibility with specific versions of a package or library. By creating a separate environment for each version of the package or library, you can ensure that the required version is installed and managed independently of other packages and dependencies. Here are some of the main pros and cons of using Conda environments:

Pros

  • provide a way to isolate software packages and dependencies in a self-contained environment, so that they do not interfere with other packages and dependencies on the same system.
  • make it easy to manage and reproduce software environments across different platforms and operating systems.
  • provide a way to manage different versions of the same software package or library.

Cons

  • can take up a lot of disk space, especially if you have many environments with many packages installed.
  • can sometimes have conflicts or dependency issues, which can be difficult to resolve.
  • can sometimes be slower to install or update packages than other package management systems.

Relation between Conda, Miniconda and Anaconda

“”

Conda Environment examples

Conda configuration

Before start of Conda using is possible to create and set up .condarc configuration file. This works similarly to .gitconfig configuration file. There are plenty of possible settings.

A very usefull could be to set a proxy servers directly for conda. More detail on .condarc.

Example of .condarc proxy settings:

proxy_servers:
    http: proxyout.example.com:8080
    https: proxyout.example.com:8080

Creation of an Environment

To create a new environment with Conda, you can use the following command:

conda create --name myenv

This will create a new environment called "myenv" with the default version of Python.

To create an environment with a specific version of Python, you can use the following command:

conda create --name myenv python=3.8

This will create a new environment called "myenv" with Python version 3.8.

To create an environment in a specific folder using Conda, you can use the --prefix or -p flag followed by the path to the desired folder. For example, to create an environment named "myenv" in the folder "/path/to/myenv", you can use the following command:

conda create --prefix /path/to/myenv myenv

This will create a new environment named "myenv" in the "/path/to/myenv" folder.

There are several reasons why you might want to create an environment in a specific folder:

  • Organization: By creating environments in separate folders, you can keep your projects and their dependencies organized and separate from each other.
  • Portability: By specifying the path to an environment folder, you can easily share your environment with others or move it to a different location.
  • Permissions: In some cases, you may not have permission to install software packages in the default Conda environment location. Creating an environment in a specific folder may allow you to install and manage packages without administrative privileges.

Overall, creating environments in specific folders can be a useful tool for managing your projects and their dependencies in a flexible and organized manner.

YML file in Conda

In the context of Conda environments, a YAML file is a text file that specifies the dependencies and configurations for a particular environment. It can include the name of the environment, channels from which to obtain packages, and a list of packages with their versions.

  1. Example of a YAML file (environment.yml) with the desired environment specifications:
name: myenv
channels:
  - conda-forge
dependencies:
  - python=3.8
  - pandas
  - matplotlib
  - scikit-learn
  1. Use the following command to create the environment from the YAML file:
conda env create --file environment.yml

This will create a new environment named "myenv" with the specified packages and Python version. You can also specify a specific environment prefix location by adding the --prefix or -p flag followed by the desired path to the command. For example:

conda env create --prefix /path/to/myenv --file environment.yml

This will create a new environment named "myenv" in the "/path/to/" directory with the specified packages and Python version. Creating environments from YAML files can be a convenient way to share and reproduce environments across different machines and operating systems.

  1. YML file export
conda env export > environment.yml

This will create a file named "environment.yml" in the current directory.

Activation and Deactivation

To activate a Conda environment, you can use the following command:

conda activate myenv

This will activate the "myenv" environment. To deactivate the environment, you can use the following command:

conda deactivate

Package Installation from Activated Environment

Once you have activated an environment, you can install packages using the following command:

conda install package_name

This will install the latest version of the package in the active environment.

To install a specific version of a package, you can use the following command:

conda install package_name=1.2.3

This will install version 1.2.3 of the package in the active environment.

Different channels for packages

Sometimes are not all packages in default anaconda channel. There are more channels or user can use standard PyPI repository.

To install packages from different channels in Conda, you can specify the channel name when installing the package using the following syntax:

conda install -c channel_name package_name

For example, to install the pydot package from the conda-forge channel, you can use the following command:

conda install -c conda-forge pydot

To install a package from PyPI, you can use the pip package manager, which is included with Conda. First, make sure that the desired environment is activated. Then, use the following command to install the package:

pip install package_name

For example, to install the tensorflow package from PyPI, you can use the following command:

pip install tensorflow

Note that when installing packages from different channels or from PyPI (Python Package Index), there may be conflicts between different package versions or dependencies. In some cases, it may be necessary to create a separate environment with a specific set of packages and dependencies to avoid conflicts.

List of Installed Environments and packages

To see a list of all the environments that you have created, you can use the following command:

conda env list

This will list all the environments along with their paths.

To see a list of all the packages that are installed in a specific environment, you can use the following command:

conda list -n myenv

This will list all the packages that are installed in the "myenv" environment.

Conda Revisions

Conda revisions allow you to rollback to a previous state of an environment by specifying a revision number. To view the revision history of an environment, use the following command:

conda list --revisions

This will show a list of all revisions for the current environment, along with the date and time of each revision. To rollback to a specific revision, use the following command:

conda install --revision revision_number

For example, to rollback to revision 2 of the current environment, use the following command:

conda install --revision 2

Deleting Tarballs

Tarballs are package files that are downloaded and cached by Conda during package installation. Over time, these tarballs can take up a significant amount of disk space. To remove all cached tarballs, use the following command:

conda clean --tarballs

Deleting Environments

To delete a specific Conda environment, use the following command:

conda remove --name env_name --all

If you have created an environment in a specific folder, you can delete it using the following command:

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

Note that deleting an environment will permanently remove all packages and data associated with that environment. Be sure to backup any important data before deleting an environment.

Return to the main page