Conda Best Practices Advanced

Follow these best practices to keep your Conda environments clean, reproducible, and conflict-free. Plus troubleshooting tips and answers to the most common questions.

Golden Rules

1. One Environment Per Project

Never install project dependencies into the base environment. Create a dedicated environment for each project to avoid dependency conflicts:

Terminal
$ conda create -n project-a python=3.11 numpy pandas
$ conda create -n project-b python=3.10 tensorflow

2. Always Specify Python Version

Explicitly set the Python version when creating an environment. This ensures reproducibility and prevents unexpected upgrades:

Terminal
# Good: explicit Python version
$ conda create -n myenv python=3.11

# Avoid: no Python version specified
$ conda create -n myenv python

3. Use environment.yml

Always maintain an environment.yml file in your project repository:

YAML
name: my-project
channels:
  - conda-forge
dependencies:
  - python=3.11
  - numpy=1.24
  - pandas=2.0
  - scikit-learn=1.3
  - pip:
    - some-pip-package==1.0

4. conda-forge First

Prefer the conda-forge channel. It has the most packages, the latest versions, and is free for all users.

5. Pin Dependencies for Production

Use exact version pins in production environments. Use conda-lock for fully reproducible lock files.

6. Clean Unused Packages

Terminal
# Remove unused cached packages and tarballs
$ conda clean --all

# See how much space can be freed
$ conda clean --all --dry-run

Troubleshooting

ProblemSolution
conda activate not working Run conda init bash (or your shell) and restart terminal
Solver takes forever Use conda config --set solver libmamba or install Mamba
Dependency conflicts Create a fresh env with all packages in one command; use --strict-channel-priority
Package not found Try -c conda-forge; if still missing, use pip install as a fallback
Disk space running low Run conda clean --all and remove unused environments
Broken environment Remove and recreate from environment.yml: conda env remove -n myenv

Frequently Asked Questions

Can I use pip and conda together?

Yes, but follow this order: install everything you can with conda first, then use pip for packages only available on PyPI. After using pip, avoid running further conda installs in the same environment if possible.

Should I use Miniconda or Anaconda?

Miniconda is recommended for most users. It gives you Conda with a minimal footprint, and you install only what you need. Anaconda is convenient if you want 250+ packages pre-installed immediately.

How do I share my environment with a colleague?

Export your environment with conda env export --from-history > environment.yml and share the YAML file. Your colleague can recreate it with conda env create -f environment.yml. For exact reproducibility, use conda-lock.

What is the difference between conda and mamba?

Mamba is a drop-in replacement for conda with a faster C++ dependency solver. Since Conda 23.10+, the libmamba solver is built into conda itself, so you may not need mamba separately.

How do I use conda in a Jupyter notebook?

Install ipykernel in your conda environment, then register it: python -m ipykernel install --user --name myenv. This makes your environment available as a kernel in Jupyter.

Can I move a conda environment to another machine?

You cannot simply copy the environment folder. Instead, export with conda env export or conda-lock and recreate on the target machine. Use --from-history for cross-platform compatibility.

Course Complete!

You now have a solid understanding of Conda for package and environment management. Apply these best practices to keep your data science and ML workflows clean and reproducible.

← Back to Course Overview