---
title: Python environments
url: https://alex.dinnouti.com/posts/2020-12-02_09-23-25_python/
date: 2020-12-02
tags: 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.


{{< figure src="https://imgs.xkcd.com/comics/python_environment.png" title="XKCD - Python Environment" link="https://xkcd.com/1987/" >}}



## 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.

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

```sh

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

```bash
export PIPENV_VENV_IN_PROJECT=true
```

## pyenv

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

```bash
# 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.

```sh
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)

```sh
pyenv install 3.8.7
pipenv --python 3.8.7
```

## References

+ [Python Environment 101. How are pyenv and pipenv different and… | by Shinichi Okada | Towards Data Science](https://towardsdatascience.com/python-environment-101-1d68bda3094d)
+ [Pipenv Cheat sheet | feici02’s tidbits](https://feici02.github.io/2017/09/24/pipenv-cheatsheet.html)
+ [pipenv — a practitioner’s cheat sheet | by Shaoqing Tan | Medium](https://medium.com/@bettercallshao/pythons-development-environment-manager-a-practitioner-s-cheat-sheet-55e44e99ac2a)
+ [The Many Faces of Python (And How to Manage Them) - Accelebrate](https://www.accelebrate.com/blog/the-many-faces-of-python-and-how-to-manage-them)





