-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Add guide on using multiple Python versions for local development #1621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6be30f9
Add guide on using multiple Python versions for local development
tswast 0bad482
Fix up whitespace in list.
tswast 5cfc02c
Add note about why these instructions differ from the recommended GCP…
tswast a739f4d
Add more example commands.
tswast 327f506
Add note about which versions to install.
tswast 35ee7d5
Add missing dot in 2.7.15
tswast 11fb03f
Rephrased global gitignore guidance.
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# 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](https://github.com/pyenv/pyenv). | ||
|
||
This guide differs from the [Google Cloud Python development | ||
instructions](https://cloud.google.com/python/setup) 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](https://brew.sh/). | ||
|
||
## Installing pyenv and pyenv-virtualenv | ||
|
||
1. Install [pyenv](https://github.com/pyenv/pyenv). | ||
|
||
I (tswast@) use [homebrew](https://brew.sh/) to install it. | ||
|
||
``` | ||
brew update | ||
brew install pyenv | ||
``` | ||
|
||
1. Install the [pyenv-virtualenv](https://github.com/pyenv/pyenv-virtualenv) | ||
plugin. | ||
|
||
``` | ||
brew install pyenv-virtualenv | ||
``` | ||
|
||
1. Append the following to your `~/.bashrc`: | ||
|
||
``` | ||
eval "$(pyenv init -)" | ||
eval "$(pyenv virtualenv-init -)"` | ||
``` | ||
|
||
Note that this also works with ZSH. | ||
|
||
1. Reload your shell. | ||
|
||
``` | ||
source ~/.bashrc | ||
``` | ||
|
||
1. 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 | ||
``` | ||
|
||
1. 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 | ||
``` | ||
|
||
1. Create a virtualenv using `pyenv virtualenv`. | ||
|
||
``` | ||
pyenv virtualenv 3.6.4 python-docs-samples | ||
``` | ||
|
||
This creates a virtualenv folder within `~/.pyenv/versions/`. | ||
|
||
1. 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 | ||
``` | ||
|
||
1. 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. | ||
|
||
1. Add `.python-version` to your [global gitignore | ||
file](https://help.github.com/articles/ignoring-files/#create-a-global-gitignore), | ||
so it wont be committed into the repository. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put the command to install it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in a739f4d