./ Alex Tech Blog / Python environments

Python environments

python cheat sheet

This post is self-service cheat sheet for Python environment and version management. One of the biggest problem with virtual environment in Python is the multiple tools available and how they play together.

The post starts from a point that you already know the options and want to test and try.

XKCD XKCD - Python Environment


In case you are using pipenv with different python version. The Pipfile tells pipenv what version to use. In case the version is not installed, Pipenv will call pyenv to install.

Example of a pipfile with a python version setting.

$ cat Pipfile
url = "https://pypi.python.org/simple"
verify_ssl = true


requests = "*"

python_version = "3.6"

Showing how pipenv will behave:

$ pipenv install
Warning: Python 3.6 was not found on your system...
Would you like us to install latest CPython 3.6 with pyenv? [Y/n]: y
Installing CPython 3.6.2 with pyenv (this may take a few minutes)...

There is an option in pipenv to install the virtual directory in the project directory instead of home subdirectory. Set the environment variable as:



Pyenv is a good tool to manage multiple Python versions in the same machine

# show local versions
pyenv versions
# list all versions
pyenv install --list
# install a specific version
pyenv install 3.9.0
# attach a version to a local folder, creates a file .python-version
pyenv local 3.9.0

Mixing pyenv with venv and pipenv (winner)

venv option

Often I see them getting out of sync and not working. The most reliable way I found was using pyenv and venv.

pyenv install 3.8.7
pyenv local 3.8.7
python3 --version
# Python 3.8.7
python3 -m venv .venv

pipenv a new option?

A different option (not tested yet)

pyenv install 3.8.7
pipenv --python 3.8.7