08 Configuring Conda & venv#
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.
Install using the instructions for your OS.
Option 2 — Anaconda Includes Conda + many preinstalled data science packages.
Install following your OS instructions.
2.2 Ensure Python is Installed (for venv)#
Check Python version:
python3 --version
venv comes bundled with Python 3.3+.
3. Execution#
3.1 Using Conda#
Create a new Conda environment
conda create --name myenv
Create a Conda environment with a specific Python version
conda create --name myenv python=3.10
Activate
conda activate myenv
Deactivate
conda deactivate
List all environments
conda info --envs
Remove an environment
conda env remove --name myenv
3.2 Using Python venv#
Create a new environment
python3 -m venv myenv
Activate environment
source myenv/bin/activate
Deactivate
deactivate
Remove a venv environment
Just delete the folder:
rm -rf myenv
4. Verification#
Check active environment
Conda:
conda env list
Active environment will be marked with *.
venv:
which python
It should point to the environment folder.
Check installed packages
pip list
Troubleshooting#
Conda#
Issue |
Cause |
Fix |
|---|---|---|
|
Conda not added to PATH |
Restart terminal or reinstall Miniconda/Anaconda |
Environment not activating |
Shell not initialized |
|
Packages won’t install |
Wrong channels |
Try |
venv#
Issue |
Cause |
Fix |
|---|---|---|
Activation script not working |
Missing permissions |
|
|
Environment not activated |
Re-run |
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.