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.
- [Optional] Install homebrew.
-
Install pyenv.
I (tswast@) use homebrew to install it.
brew update brew install pyenv
-
Install the pyenv-virtualenv plugin.
brew install pyenv-virtualenv
-
Append the following to your
~/.bashrc
:eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"`
Note that this also works with ZSH.
-
Reload your shell.
source ~/.bashrc
-
Verify that you are now using the pyenv Python shim.
$ which python /Users/tswast/.pyenv/shims/python
-
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
-
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)
-
Change to the desired source directory.
cd ~/src/python-docs-samples
-
Create a virtualenv using
pyenv virtualenv
.pyenv virtualenv 3.6.4 python-docs-samples
This creates a virtualenv folder within
~/.pyenv/versions/
. -
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
-
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 likepython36
andpython27
to your PATH, which nox uses when picking Python interpreters. -
Add
.python-version
to your global gitignore file, so it wont be committed into the repository.