pyenv#
The pyenv tool allows you to install and manage multiple versions of the Python interpreter. It also supports alternative Python implementations out-of-the-box, allowing you to test your code in different environments.
Under the hood, pyenv is a collection of shell scripts. In particular python-build provides the pyenv install
functionality to download the source code, compile and install a given version of the Python executable.
Installation#
The root
user needs to install the packages required to build Python from source.
# apt install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
No admin rights are required to install and use pyenv
itself
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ cd ~/.pyenv && src/configure && make -C src && cd ..
$ ~/.pyenv/bin/pyenv --help
Note that you will have to adapt your $PATH
if you want to use the pyenv
command directly in your shell, without prepending the full path ~/.pyenv/bin/pyenv
.
We recommend to use a package manager like homebrew.
$ brew install pyenv
Consider using the pyenv-win port on Windows.
Usage#
Start by listing the ~500 Python interpreters that can be installed with pyenv
$ pyenv install --list
Then install any version you need
$ pyenv install <version>
The interpreter will be installed into ~/.pyenv/versions/<version>/
. You can use the full path to the binaries to run a specific python interpreter,
$ ~/.pyenv/versions/<version>/bin/python your_python_script.py
or to create a new virtual environment and install packages within.
cd some_python_project/
~/.pyenv/versions/<version>/bin/python -m venv .venv
source .venv/bin/activate
pip3 install -r requirements.txt
Feel free to adapt $PYENV_ROOT
to change the install location. Refer to the pyenv documentation for other ways to manage the Python version in the shell or per-project.