Python Environments

Table of contents
  1. Why do you need them?
  2. Python version manager vs Virtual environments
    1. Typical use case
  3. Notes / sanity check

Why do you need them?

Every Python project comes with different requirements.

For example:

 PythonLibrary1Library2Library3
Project A3.6x1.1.22.3.0
Project B3.10x2.3.13.0.1
Project C2.71.4.01.1.2x

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 python3 will point to the python binary
  • which pip / which pip3 will point the pip binary
  • pip -V / pip3 -V will point to the site-packages
  • conda run which python / conda run python -V does the expected for the base conda env or the active env
  • pipenv run which python / pipenv run python -V does the expected for the current root directory env
  • However, pipenv run pip -V will create an env for the cwd and add pip to the Pipfile for cwd

Table of contents