>./ Alex Tech Blog / Python environments

Python environments

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 - Python Environment

pipenv

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$ cat Pipfile
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true

[dev-packages]

[packages]
requests = "*"

[requires]
python_version = "3.6"

Showing how pipenv will behave:

1
2
3
4
5
6

$ 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:

1
export PIPENV_VENV_IN_PROJECT=true

pyenv

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

1
2
3
4
5
6
7
8
# 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.

1
2
3
4
5
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)

1
2
pyenv install 3.8.7
pipenv --python 3.8.7

References

#aws #cheat sheet #codecommit #esri #github #honeycode #javascript #jupyter #linux #machine learning #pandas #php #python #salesforce #ssh