Environment Management Intermediate
Conda environments are isolated spaces where you can install specific versions of Python and packages without affecting other projects. This is the core feature that makes Conda indispensable for data science workflows.
Creating Environments
Create a new environment with a specific Python version:
# Create environment with a specific Python version $ conda create -n myenv python=3.11 # Create with packages pre-installed $ conda create -n datascience python=3.11 numpy pandas matplotlib # Create with a specific Python and channel $ conda create -n mlenv python=3.11 -c conda-forge # Create in a specific directory (not default location) $ conda create --prefix ./envs/myproject python=3.11
Activating and Deactivating
# Activate an environment $ conda activate myenv (myenv) $ # Prompt changes to show active env # Deactivate (return to base) $ conda deactivate # Switch directly to another environment $ conda activate other_env
conda install. Otherwise, packages get installed into the base environment.
Listing Environments
# List all environments $ conda env list # or equivalently $ conda info --envs base * /home/user/miniconda3 myenv /home/user/miniconda3/envs/myenv datascience /home/user/miniconda3/envs/datascience
Removing Environments
# Remove an environment and all its packages $ conda env remove -n myenv # Or using conda remove with --all flag $ conda remove -n myenv --all
Cloning Environments
Create an exact copy of an existing environment:
# Clone an existing environment $ conda create --name myenv_copy --clone myenv
Exporting and Sharing Environments
Export your environment to a YAML file so others can recreate it:
# Export the active environment (includes all dependencies) $ conda env export > environment.yml # Export only explicitly installed packages (cross-platform friendly) $ conda env export --from-history > environment.yml
Here is what an environment.yml file looks like:
name: datascience channels: - conda-forge - defaults dependencies: - python=3.11 - numpy=1.24 - pandas=2.0 - scikit-learn=1.3 - matplotlib=3.7 - jupyter=1.0 - pip: - some-pip-only-package==1.2.3
Creating from YAML
# Create environment from a YAML file $ conda env create -f environment.yml # Update an existing environment from YAML $ conda env update -f environment.yml --prune
Stacking Environments
You can activate an environment on top of another using --stack:
# Stack environments (packages from both are available) $ conda activate --stack another_env
~/miniconda3/envs/ (or ~/anaconda3/envs/). You can create environments in custom locations using --prefix instead of -n.
Environment Workflow Summary
| Task | Command |
|---|---|
| Create environment | conda create -n myenv python=3.11 |
| Activate | conda activate myenv |
| Deactivate | conda deactivate |
| List environments | conda env list |
| Remove environment | conda env remove -n myenv |
| Clone environment | conda create --clone myenv -n copy |
| Export to YAML | conda env export > environment.yml |
| Create from YAML | conda env create -f environment.yml |
Practice Time
Try creating an environment, installing a few packages, exporting it to YAML, then recreating it from the YAML file. Next, learn how to manage packages inside your environments.
Next: Package Management →Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX