How to use the command `conda` (with examples)
conda
is a command-line tool that is used for package, dependency, and environment management for any programming language. It allows users to create and manage virtual environments, install packages, and manage dependencies.
Use case 1: Create a new environment, installing named packages into it
Code:
conda create --name environment_name python=3.9 matplotlib
Motivation: This use case is helpful when you want to create a new environment for a specific project or application and install specific packages into it.
Explanation:
conda create
: This is the subcommand used to create a new environment.--name environment_name
: Specifies the name of the environment to be created.python=3.9 matplotlib
: Specifies the packages to be installed into the environment. In this example, it installs Python version 3.9 and thematplotlib
package.
Example output:
# Output:
# ...
# Proceed ([y]/n)? y
# ...
# Installing packages:
# python-3.9.6-hdb3f193_2 ...
# matplotlib-base-3.4.3-py39h9bdec22_0 ...
# ...
Use case 2: List all environments
Code:
conda info --envs
Motivation: This use case helps to view a list of all the environments created using conda
. It can be useful when you have multiple environments and need to verify their names or locations.
Explanation:
conda info
: This subcommand provides information about Conda, including the list of environments.--envs
: This flag is used to specifically list all the environments.
Example output:
# Output:
# # conda environments:
# #
# base * C:\Anaconda3
# environment_name C:\Anaconda3\envs\environment_name
# ...
Use case 3: Load an environment
Code:
conda activate environment_name
Motivation: This use case is used to switch to a specific environment. It allows you to work with a specific set of packages and dependencies for a particular project or application.
Explanation:
conda activate
: This subcommand is used to activate (or load) an environment.environment_name
: Specifies the name of the environment to be loaded.
Example output: This command does not produce any output. It simply activates the specified environment.
Use case 4: Unload an environment
Code:
conda deactivate
Motivation: This use case helps to unload the current environment and return to the base environment. It is useful when you want to switch between environments or no longer need to work in a specific environment.
Explanation:
conda deactivate
: This subcommand is used to deactivate (or unload) the current environment.
Example output: This command does not produce any output. It simply deactivates the current environment.
Use case 5: Delete an environment (remove all packages)
Code:
conda remove --name environment_name --all
Motivation: This use case is useful when you want to remove an environment completely, including all the packages installed in it. It allows you to clean up your system and free up disk space.
Explanation:
conda remove
: This subcommand is used to remove an environment.--name environment_name
: Specifies the name of the environment to be removed.--all
: This flag indicates that all packages installed in the environment should be removed.
Example output:
# Output:
# Remove all packages in environment C:\Anaconda3\envs\environment_name:
#
# y
#
# Done!
Use case 6: Install packages into the current environment
Code:
conda install python=3.4 numpy
Motivation: This use case is helpful when you want to install specific packages into the current environment. It allows you to add new packages or update existing ones.
Explanation:
conda install
: This subcommand is used to install packages.python=3.4 numpy
: Specifies the packages to be installed. In this example, it installs Python version 3.4 and thenumpy
package.
Example output:
# Output:
# Solving environment: done
# ...
# numpy-1.22.0-py34h2e9c7e3_0 ...
# ...
Use case 7: List currently installed packages in the current environment
Code:
conda list
Motivation: This use case helps to view a list of all the packages installed in the current environment. It can be useful when you need to check the installed versions or troubleshoot dependency issues.
Explanation:
conda list
: This subcommand lists all the packages installed in the current environment.
Example output:
# Output:
# # packages in environment at C:\Anaconda3:
# #
# # Name Version Build Channel
# numpy 1.22.0 py39h0866f7a_0
# ...
Use case 8: Delete unused packages and caches
Code:
conda clean --all
Motivation: This use case helps to clean up unused packages and caches that accumulate over time. It can help free up disk space and ensure a clean environment.
Explanation:
conda clean
: This subcommand is used to clean unused packages and caches.--all
: This flag indicates that all unused packages and caches should be cleaned.
Example output:
# Output:
# ...
# Clean complete
# ...
Conclusion:
In this article, we explored various use cases of the conda
command. We learned how to create and manage environments, install packages, and clean up unused resources. Understanding these use cases can greatly enhance your productivity in managing packages, dependencies, and environments using conda
.