|
| 1 | +# Setting up a Mac development environment with pyenv and pyenv-virtualenv |
| 2 | + |
| 3 | +In this guide, you'll set up a local Python development environment with |
| 4 | +multiple Python versions, managed by [pyenv](https://github.com/pyenv/pyenv). |
| 5 | + |
| 6 | +This guide differs from the [Google Cloud Python development |
| 7 | +instructions](https://cloud.google.com/python/setup) because developers of |
| 8 | +samples and libraries need to be able to use multiple versions of Python to |
| 9 | +test their code. |
| 10 | + |
| 11 | +## Before you begin |
| 12 | + |
| 13 | +1. [Optional] Install [homebrew](https://brew.sh/). |
| 14 | + |
| 15 | +## Installing pyenv and pyenv-virtualenv |
| 16 | + |
| 17 | +1. Install [pyenv](https://github.com/pyenv/pyenv). |
| 18 | + |
| 19 | + I (tswast@) use [homebrew](https://brew.sh/) to install it. |
| 20 | + |
| 21 | + ``` |
| 22 | + brew update |
| 23 | + brew install pyenv |
| 24 | + ``` |
| 25 | +
|
| 26 | +1. Install the [pyenv-virtualenv](https://github.com/pyenv/pyenv-virtualenv) |
| 27 | + plugin. |
| 28 | +
|
| 29 | + ``` |
| 30 | + brew install pyenv-virtualenv |
| 31 | + ``` |
| 32 | +
|
| 33 | +1. Append the following to your `~/.bashrc`: |
| 34 | +
|
| 35 | + ``` |
| 36 | + eval "$(pyenv init -)" |
| 37 | + eval "$(pyenv virtualenv-init -)"` |
| 38 | + ``` |
| 39 | +
|
| 40 | + Note that this also works with ZSH. |
| 41 | +
|
| 42 | +1. Reload your shell. |
| 43 | +
|
| 44 | + ``` |
| 45 | + source ~/.bashrc |
| 46 | + ``` |
| 47 | +
|
| 48 | +1. Verify that you are now using the pyenv Python shim. |
| 49 | +
|
| 50 | + ``` |
| 51 | + $ which python |
| 52 | + /Users/tswast/.pyenv/shims/python |
| 53 | + ``` |
| 54 | +
|
| 55 | +## Installing multiple Python versions |
| 56 | +
|
| 57 | +
|
| 58 | +1. See the available Python versions with |
| 59 | +
|
| 60 | + ``` |
| 61 | + pyenv install --list |
| 62 | + ``` |
| 63 | +
|
| 64 | + The Python versions are at the top of the long list. If the Python |
| 65 | + version you want isn't listed, you may need to upgrade your pyenv with |
| 66 | + homebrew. |
| 67 | +
|
| 68 | + ``` |
| 69 | + brew update |
| 70 | + brew upgrade pyenv |
| 71 | + ``` |
| 72 | +
|
| 73 | +1. Compile the necessary Python versions with pyenv. Use the latest release |
| 74 | + of the versions you wish to test against. |
| 75 | +
|
| 76 | + As of August 8, 2018 my (tswast@) Python versions are: |
| 77 | +
|
| 78 | + * 2.7.15 (latest 2.7.x release) |
| 79 | + * 3.5.4 (latest 3.5.x release) |
| 80 | + * 3.6.4 (latest 3.6.x release) |
| 81 | + * 3.7.0 (latest 3.7.x release) |
| 82 | +
|
| 83 | +## Using pyenv and pyenv-virtualenv to manage your Python versions |
| 84 | +
|
| 85 | +1. Change to the desired source directory. |
| 86 | +
|
| 87 | + ``` |
| 88 | + cd ~/src/python-docs-samples |
| 89 | + ``` |
| 90 | +
|
| 91 | +1. Create a virtualenv using `pyenv virtualenv`. |
| 92 | +
|
| 93 | + ``` |
| 94 | + pyenv virtualenv 3.6.4 python-docs-samples |
| 95 | + ``` |
| 96 | +
|
| 97 | + This creates a virtualenv folder within `~/.pyenv/versions/`. |
| 98 | +
|
| 99 | +1. Set the local Python version(s) with `pyenv local` |
| 100 | +
|
| 101 | + ``` |
| 102 | + # pyenv local [name of virtualenv] [list of python versions to use] |
| 103 | + pyenv local python-docs-samples 3.6.4 3.7.0 3.5.4 2.7.15 |
| 104 | + ``` |
| 105 | +
|
| 106 | +1. Now, when you `cd` into the source directory or a subdirectory within it, |
| 107 | + pyenv will make your virtualenv the default Python. Since you specified |
| 108 | + more than one version, it will also add binaries like `python36` and |
| 109 | + `python27` to your PATH, which nox uses when picking Python interpreters. |
| 110 | +
|
| 111 | +1. Add `.python-version` to your [global gitignore |
| 112 | + file](https://help.github.com/articles/ignoring-files/#create-a-global-gitignore), |
| 113 | + so it wont be committed into the repository. |
0 commit comments