Conda Tutorial: Environment and Package Management
En esta página
- 1. Installation
- 2. Managing Environments
- Create an Environment
- Activate and Deactivate an Environment
- List Environments
- Remove an Environment
- 3. Managing Packages
- Install Packages
- Install from other channels
- List Installed Packages
- Update Packages
- Remove Packages
- 4. Sharing Environments
- Export an Environment
- Create an Environment from a File
Conda is an open-source package and environment management system that runs on Windows, macOS, and Linux. It is widely used in the data science and machine learning communities.
1. Installation
We recommend installing Miniconda, a minimal installer for conda. It is smaller and faster to install than the full Anaconda distribution.
-
Download the installer: Go to the Miniconda download page and download the appropriate installer for your operating system.
-
Run the installer: Follow the instructions for your OS. It's recommended to allow the installer to initialize conda, which will make it available in your terminal.
-
Verify installation: Open a new terminal and run:
conda --versionYou should see the installed conda version.
2. Managing Environments
A conda environment is a directory that contains a specific collection of conda packages that you have installed.
Create an Environment
To create a new environment, use conda create. It's good practice to specify the Python version you want to use.
# Creates an environment named 'myenv' with Python 3.12
conda create --name myenv python=3.12
You can also install packages at the same time:
conda create --name myenv python=3.12 numpy pandas jupyter
Activate and Deactivate an Environment
Before you can use an environment, you need to activate it.
# Activate the environment
conda activate myenv
Your terminal prompt should change to show the name of the active environment.
To deactivate the current environment and return to the base environment:
# Deactivate the environment
conda deactivate
List Environments
To see a list of all your environments:
conda env list
The active environment will be marked with an asterisk (*).
Remove an Environment
To delete an environment and all the packages installed in it:
# Make sure the environment is not active
conda deactivate
# Remove the environment
conda env remove --name myenv
3. Managing Packages
With your environment activated, you can install, update, and remove packages.
Install Packages
Use conda install to install packages from the default Anaconda channel.
# Activate your environment first
conda activate myenv
# Install packages
conda install numpy pandas scikit-learn
You can also specify package versions:
conda install numpy=1.26.0
Install from other channels
Sometimes packages are not available in the default channel. A popular channel is conda-forge.
conda install -c conda-forge some-package
List Installed Packages
To see all packages installed in the current environment:
conda list
Update Packages
To update a specific package:
conda update numpy
To update all packages in the environment:
conda update --all
Remove Packages
To uninstall a package:
conda remove scikit-learn
4. Sharing Environments
You can share your environment with others by exporting its specification to a YAML file.
Export an Environment
- Activate the environment you want to export.
conda activate myenv - Export the environment to a file (commonly named
environment.yml).conda env export > environment.yml
This file can be shared, and others can replicate your environment.
Create an Environment from a File
To create an environment from an environment.yml file:
conda env create -f environment.yml
This will create a new environment with the same name and packages as specified in the file.