Getting started 4. Environment management
Environment management
Managing your Python environment matters when projects need different Python versions or package versions.
| Problem | Solution |
|---|---|
| Different Python versions are needed | Create isolated environments |
| Projects need different package versions | Manage dependencies separately |
| Version conflicts can break code | Keep each project isolated |
| Setups are hard to reproduce | Share the environment definition |
Solution 1: venv
venv is the built-in Python tool for creating virtual environments.
python -m venv myenv
Activate it on macOS or Linux:
source myenv/bin/activate
Deactivate it:
deactivate
Solution 2: Conda
Conda manages both Python and non-Python dependencies.
conda create -n myenv python=3.10
conda activate myenv
conda deactivate
You can also export an environment:
conda env export > environment.yml
Installing packages
With conda:
conda install -n myenv package_name
With pip inside an activated environment:
pip install package_name