Skip to content

Instantly share code, notes, and snippets.

@cereblanco
Last active May 17, 2021 12:46
Show Gist options
  • Save cereblanco/d9b19e55d7b63e5be1d43732e9d2f942 to your computer and use it in GitHub Desktop.
Save cereblanco/d9b19e55d7b63e5be1d43732e9d2f942 to your computer and use it in GitHub Desktop.
PyEnv CheatSheet

PyEnv Cheatsheet

IMPORTANT!

  • pyenv manages multiple versions of Python
  • pyenv-virtualenv manages virtual environments for different versions of Python

Installation

  • brew install pyenv installs pyenv
  • brew install pyenv-virtualenv installs pyenv-virtualenv -- a plugin that provides features to manage virtualenvs
  • pyenv install --list | grep " 3\.[678]" lists all Cpython versions
  • pyenv install --list | grep "jython" lists all Jython versions
  • pyenv install --list | grep "pypy" lists all PyPy versions
  • pyenv install 3.8.0 installs Python 3.8.0
  • pyenv uninstall 3.8.0 uninstalls Python 3.8.0

How to use pyenv?

  • pyenv versions lists all installed versions on our machine. The * indicates the currently active Python version. In the case below, we are using Python 3.8.0
    * 3.8.0 (set by /Users/cereblanco/.pyenv/version)
    3.9.0
  • pyenv global 3.9.0 sets the active version to Python 3.9.0 globally on our machine
  • pyenv local 3.8.0 sets the active version to Python 3.8.0 locally in the current directory. This is often used when we are working on a specific application-- it sets the active version locally in the project's root directory.
  • pyenv shell 3.8-dev sets the active version to Python 3.8-dev on the currently active shell/cli. It is deactivated once the shell is exited.

How to use virtualenv and pyenv?

  • cd path-to-my-project/myproject changes the directory to the given project

  • pyenv virtualenv <python_version> <virtualenv_name> creates a virtualenv named virtualenv_name with python_version

    Example! pyenv virtualenv 3.8.0 myproject creates a virtualenv myproject using python 3.8.0

  • pyenv local myproject activates locally the myproject environment in the current directory (myproject's root directory)

  • pyenv activate myproject activates myproject environment

  • pyenv deactivate deactivates the myproject environment

  • pyenv local --unset removes the myproject environment from the local environment

How to setup pyenv and virtualenv in another project, or multiple projects

Tips!

  1. Setup a separate local environment for each project
  2. When working on a project, we activate the specific environment for that project
  • cd path-to-my-project/another_project changes the directory to another specific application, for instance another_project
  • pyenv virtualenv 3.8.0 another_project creates a virtualenv for another_project
  • pyenv local another_project sets this another_project environment locally in the current directory
  • pyenv activate another_project activates the another_project environment
  • pyenv deactivate deactivates the another_project environment

How to use multiple versions of python simultaneously in a single project

For instance, we want to use Python 3.9.0 and also wanted to check if the current project still works on the newest stable build Python 3.10 (Python 3.10-0a7)

  • cd path-to-my-project/myproject let's continue working on myproject
  • python --version shows the currently active Python version
    myproject // python3.8
  • pyenv virtualenv 3.10.0a7 myproject-another-env creates another environment myproject-another-env using Python 3.10.0a7
  • pyenv local myproject myproject-another-env sets the two environments myproject myproject-another-env locally
  • pyenv local shows the environments in the current directory
    myproject // python3.8
    myproject-another-env // python3.10
    In our current myproject directory, we can directly use python.3.8 and python3.10 simultaneously
    $ python3.10 --version
    $ python3.8 --version

Resources

[1] https://realpython.com/intro-to-pyenv/ [2] https://github.com/pyenv/pyenv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment