Skip to content

Instantly share code, notes, and snippets.

@naviat
Created June 26, 2021 07:26
Show Gist options
  • Save naviat/4bfc21f74c125682f6e0d31f49a9722a to your computer and use it in GitHub Desktop.
Save naviat/4bfc21f74c125682f6e0d31f49a9722a to your computer and use it in GitHub Desktop.
pyenv cheat sheet

Python environment cheatsheet

Tool for creating isolated Python environments

The common use-cases:

  • Handling different versions of same libraries per each service
  • Same plus no access to site-packages

In all these cases, virtualenv can help. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

Extension scripts for virtualenv

  1. Organizes all of your virtual environments in one place
  2. Wrappers for managing your virtual environments (create, delete, copy)
  3. Use a single command to switch between environments
  4. Tab completion for commands that take a virtual environment as argument

Tool for switching between different versions of Python interpreter

Intercepts Python commands using shim executables injected into your PATH, determines which Python version has been specified by your application, and passes your commands along to the correct Python installation

command reference

PyEnv plugin that provides features to manage virtualenvs and conda environments for Python on UNIX-like systems

Features:

  • additional commands activate, deactivate, ...
  • auto activation and deactivation on directory enter/leave events (using .python-version file)

PyEnv plugin which provides a pyenv virtualenvwrapper command to manage virtualenvs with virtualenvwrapper

Install

virtualenv + virtualenvwrapper

sudo -H pip install virtualenv

~/.bashrc

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
mkdir -p $PROJECT_HOME

pyenv + pyenv-virtualenv

curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

~/.bashrc

export PATH="$HOME/.pyenv/bin:$PATH"
export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV=1
eval "$(pyenv init -)"
source ~/.bashrc

pyenv-virtualenvwrapper

git clone https://github.com/yyuu/pyenv-virtualenvwrapper.git $(pyenv root)/plugins/pyenv-virtualenvwrapper

Satisfy pyenv build requirements

sudo apt-get install -y \
    make \
    build-essential \
    libssl-dev \
    zlib1g-dev \
    libbz2-dev \
    libreadline-dev \
    libsqlite3-dev \
    wget \
    curl \
    llvm \
    libncurses5-dev \
    libncursesw5-dev \
    xz-utils \
    libffi-dev \
    libssl1.0-dev

Install some basic Python version for virtualenvwrapper and install it inside

pyenv install <basic_python_version>
pyenv global <basic_python_version>
pip install virtualenvwrapper

~/.bashrc

pyenv virtualenvwrapper_lazy

Update

pyenv update

Uninstall

rm -rf $(pyenv root)
sudo -H pip uninstall -y virtualenv virtualenvwrapper

Remove added ~/.bashrc records related to virtualenv/virtualenvwrapper/pyenv

Workflow: New project

  1. Install latest CPython 2.7

    pyenv install 2.7-dev
  2. Create new project and environment (both with the same name project_name)

    mkproject <project_name>
  3. Set Python version

    pyenv shell 2.7-dev
  4. Working on a project ...

  5. Exit environment

    source deactivate
  6. Enter environment workon <project_name>

    workon <project_name>
  7. Working on a project ...

Workflow: Existing non-virtualized project

  1. Switch to project directory
    cd <project_path>
  2. Freeze requirements based on imports (if needed)
    pipreqs .
  3. Set Python version required by the project
    pyenv shell 2.7-dev
  4. Create virtual environment (if needed)
    mkvirtualenv -r <requirements_file_path> <virtualenv_name>
  5. Bind an existing virtualenv to an existing project
    setvirtualenvproject <virtualenv_path> <project_path>
    or just
    setvirtualenvproject
    if environment activated and PWD is a project root directory
  6. Working on a project ...
  7. Exit environment
    source deactivate

Workflow: Temporary environment

  1. Set Python version for the project
    pyenv shell 2.7-dev
  2. Create temporary virtual environment
    mktmpenv
  3. Working ...
  4. Exit environment (environment folder will be deleted on deactivation)
    source deactivate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment