Python Environments
Table of contents
Why do you need them?
Every Python project comes with different requirements.
For example:
| Python | Library1 | Library2 | Library3 | |
|---|---|---|---|---|
| Project A | 3.6 | x | 1.1.2 | 2.3.0 |
| Project B | 3.10 | x | 2.3.1 | 3.0.1 |
| Project C | 2.7 | 1.4.0 | 1.1.2 | x |
As the number of projects grow, managing different versions of Python and their packages are going to be increasingly difficult.
Switching between them and resolving conflicts is one hassle, but removing them after use is also a pain.
Environments, however, remembers the context of a project, and keeps them independent of other project’s context.
Hence, project collaboration and management becomes much easier with environments.
Python version manager vs Virtual environments
It can be confusing because they all go by the name environment.
Long story short,
- Version manager: manages Python versions
- Virtual environment: manages libraries
You’ll probably end up needing both.
Typical use case
You install and select a Python version to use with a version manager.
Then create a virtual environment for a project using that Python version.
For example (not comprehensive):
- pyenv: version manager
- Conda: version manager + virtual environment
- venv: virtual environment
- Pipenv: virtual environment
- Poetry: virtual environment
Notes / sanity check
which python/which python3will point to the python binarywhich pip/which pip3will point the pip binarypip -V/pip3 -Vwill point to thesite-packagesconda run which python/conda run python -Vdoes the expected for the base conda env or the active envpipenv run which python/pipenv run python -Vdoes the expected for the current root directory env- However,
pipenv run pip -Vwill create an env for the cwd and add pip to thePipfilefor cwd