[Anaconda] Complete Guide to conda Commands: Creating/Managing Virtual Environments and Package Operations

Anaconda is a distribution that allows you to set up Python itself along with libraries frequently used for data analysis and scientific computing, such as NumPy, pandas, and Matplotlib, all at once. To manage these, we use the conda command.

Below, I have systematically organized the major commands, ranging from building virtual environments to package management. This guide is summarized in a format ready for practical use.

目次

Table of Contents

  1. Creating Virtual Environments (conda create)
  2. Operating Virtual Environments (activate / info / remove)
  3. Package Management (install / update / remove)
  4. Saving and Reproducing Environments (Export)
    • Output current environment configuration to file
    • Reproduce (create) environment from file
  5. Note: Using pip and conda together

1. Creating Virtual Environments (conda create)

Create a new virtual environment to isolate Python versions and libraries for each project.

PurposeCommand
Create specifying packagesconda create --name <env_name> <package_name>
Create specifying Python versionconda create --name <env_name> python=3.9
Clone an existing environmentconda create --name <env_name> --clone <source_env>
Clone the base environmentconda create --name <env_name> --clone base

Execution Example: Creating an environment named data_analysis containing Python 3.10 and pandas.

conda create --name data_analysis python=3.10 pandas

2. Operating Virtual Environments (activate / info / remove)

To use a created environment, you need to switch to it (activate it).

PurposeCommand
Activate environmentconda activate <env_name>
Deactivate (Return to base)conda deactivate
List created environmentsconda info --envs
Delete environmentconda remove --name <env_name> --all

Supplement: In the output of conda info --envs, the environment marked with * is the currently active environment.

3. Package Management (install / update / remove)

Add or update libraries. Basically, this is done for the active environment, but it can also be specified externally using the --name option.

PurposeCommand
Search for a packageconda search <keyword>
Install a packageconda install --name <env_name> <package_name>
Update a packageconda update --name <env_name> <package_name>
Update conda itselfconda update conda
Uninstall a packageconda remove --name <env_name> <package_name>
Check installed packagesconda list --name <env_name>

Execution Example: Installing scikit-learn into the current environment.

conda install scikit-learn

4. Saving and Reproducing Environments (Export)

Use this when you want to build the same environment for team members or migrate the environment to another machine.

Output current environment configuration to file

Write the list of installed packages to a text file.

conda list --name <env_name> --export > package-list.txt

Note: The following command, which outputs in YAML format to improve compatibility between OSs, is also frequently used.

conda env export --name <env_name> > environment.yml

Reproduce (create) environment from file

Build a new environment based on the output file.

conda create --name <new_env_name> --file package-list.txt

Note: Using pip and conda together

Within an Anaconda environment, prioritize using conda install as a general rule. Use pip install only for packages that are not provided by conda. Mixing these disorderly can break dependencies and cause the environment to malfunction.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次