Goal
Learn how to create, activate, manage, and remove isolated Python environments using Conda and Python’s built-in venv. This ensures clean, reproducible setups for different projects without dependency conflicts.
Prerequisites
Steps
1. Introduction
When working on multiple Python projects, you often need different versions of specific packages or Python itself. Environments are ways to separate versions of packages for different projects and keep different versions isolated.
Why do you need this? Imagine you’re working on two projects: one uses NumPy 1.20 and the other uses NumPy 1.24. If you install both globally on your computer, they’ll conflict. Virtual environments let you have separate, isolated Python installations for each project. Conda is a comprehensive environment manager that can manage both Python versions and packages. Meanwhile, venv is Python’s built-in, lightweight virtual environment tool that works within a single project directory.
2. Setup
2.1 Install Conda
Option 1 — Miniconda (recommended) A minimal, lightweight installer for Conda.
- Download: https://docs.conda.io/en/latest/miniconda.html
- Install using the instructions for your OS.
Option 2 — Anaconda Includes Conda + many preinstalled data science packages.
- Download: https://www.anaconda.com/products/individual
- Install following your OS instructions.
2.2 Ensure Python is Installed (for venv)
Check Python version:
python3 --versionvenv comes bundled with Python 3.3+.
3. Execution
3.1 Using Conda
Create a new Conda environment
conda create --name myenvCreate a Conda environment with a specific Python version
conda create --name myenv python=3.10Activate
conda activate myenvDeactivate
conda deactivateList all environments
conda info --envsRemove an environment
conda env remove --name myenv3.2 Using Python venv
Create a new environment
python3 -m venv myenvActivate environment
source myenv/bin/activateDeactivate
deactivateRemove a venv environment
Just delete the folder:
rm -rf myenv4. Verification
Check active environment
Conda:
conda env listActive environment will be marked with *.
venv:
which pythonIt should point to the environment folder.
Check installed packages
pip listTroubleshooting
Conda
| Issue | Cause | Fix |
|---|---|---|
conda: command not found | Conda not added to PATH | Restart terminal or reinstall Miniconda/Anaconda |
| Environment not activating | Shell not initialized | conda init bash (or zsh, fish, powershell) |
| Packages won’t install | Wrong channels | Try conda-forge: conda install -c conda-forge <package> |
venv
| Issue | Cause | Fix |
|---|---|---|
| Activation script not working | Missing permissions | chmod +x myenv/bin/activate |
python still points to system Python | Environment not activated | Re-run source myenv/bin/activate |
Next Steps
You are ready to start working with environemnts, that’s great! However, many times we want to share code among the collaboration and avoid the ‘it-works-on-my-computer’ problem. Environments are a tool to isolate package versions, but they are also heavy. That’s why most of the repositories should have a requirements.txt file, specifing which package you need to install in a conda environment or venv. Anyways, there even is a more straightforward solution 16_docker.