Skip to content

Latest commit

 

History

History
113 lines (77 loc) · 2.9 KB

MAC_SETUP.md

File metadata and controls

113 lines (77 loc) · 2.9 KB

Setting up a Mac development environment with pyenv and pyenv-virtualenv

In this guide, you'll set up a local Python development environment with multiple Python versions, managed by pyenv.

This guide differs from the Google Cloud Python development instructions because developers of samples and libraries need to be able to use multiple versions of Python to test their code.

Before you begin

  1. [Optional] Install homebrew.

Installing pyenv and pyenv-virtualenv

  1. Install pyenv.

    I (tswast@) use homebrew to install it.

    brew update
    brew install pyenv
    
  2. Install the pyenv-virtualenv plugin.

    brew install pyenv-virtualenv
    
  3. Append the following to your ~/.bashrc:

    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"`
    

    Note that this also works with ZSH.

  4. Reload your shell.

    source ~/.bashrc
    
  5. Verify that you are now using the pyenv Python shim.

    $ which python
    /Users/tswast/.pyenv/shims/python
    

Installing multiple Python versions

  1. See the available Python versions with

    pyenv install --list
    

    The Python versions are at the top of the long list. If the Python version you want isn't listed, you may need to upgrade your pyenv with homebrew.

    brew update
    brew upgrade pyenv
    
  2. Compile the necessary Python versions with pyenv. Use the latest release of the versions you wish to test against.

    As of August 8, 2018 my (tswast@) Python versions are:

    • 2.7.15 (latest 2.7.x release)
    • 3.5.4 (latest 3.5.x release)
    • 3.6.4 (latest 3.6.x release)
    • 3.7.0 (latest 3.7.x release)

Using pyenv and pyenv-virtualenv to manage your Python versions

  1. Change to the desired source directory.

    cd ~/src/python-docs-samples
    
  2. Create a virtualenv using pyenv virtualenv.

    pyenv virtualenv 3.6.4 python-docs-samples
    

    This creates a virtualenv folder within ~/.pyenv/versions/.

  3. Set the local Python version(s) with pyenv local

    # pyenv local [name of virtualenv] [list of python versions to use]
    pyenv local python-docs-samples 3.6.4 3.7.0 3.5.4 2.7.15
    
  4. Now, when you cd into the source directory or a subdirectory within it, pyenv will make your virtualenv the default Python. Since you specified more than one version, it will also add binaries like python36 and python27 to your PATH, which nox uses when picking Python interpreters.

  5. Add .python-version to your global gitignore file, so it wont be committed into the repository.