Environments

2020

Python distribution and dependency management can be a bit of a nightmare1 xkcd: Python Environment, thus it is prudent to unambiguously specify what a project requires.

Python version

The pyenv2 Simple Python Version Management: pyenv project provides global (system-level) and local (project-level) python version control.

Installation

On macOS:

brew install pyenv

On other platforms see here.

Commands

View installed versions3 Lists all Python versions known to pyenv, and shows an asterisk next to the currently active version.:

pyenv versions

View and install available distributions using pyenv install4 Install a Python version.

pyenv install --list
pyenv install 3.8.1

Set a global default distribution5 Sets the global version of Python to be used in all shells.:

pyenv global 3.8.1

Set a project-level distribution6 Sets a local application-specific Python version.:

pyenv local 3.8.1

Python packages

The pipenv7 Pipenv: Python Development Workflow for Humans. project provides project-level package installation à la npm, renv, etc.

Installation

On macOS:

brew install pipenv

On other platforms see here.

Commands

Install/uninstall packages works as it does with pip, just with pipenv instead:

pip install pandas
pip uninstall pandas

Launching environments

Launch a shell with the local environment:

pipenv shell

Run a command in the local virtual environment:

pipenv run <command>
pipenv run juptyer notebook
pipenv run python script.py