Skip to content

Commit 277d267

Browse files
authored
Add guide on using multiple Python versions for local development (GoogleCloudPlatform#1621)
* Add guide on using multiple Python versions for local development * Add note about why these instructions differ from the recommended GCP setup.
1 parent a806f2c commit 277d267

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

CONTRIBUTING.md

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ be able to accept your pull requests.
2929
1. Ensure that your code has an appropriate set of unit tests which all pass.
3030
1. Submit a pull request.
3131

32+
## Setting up a development environment
33+
34+
* [Mac development environment guide](MAC_SETUP.md)
35+
3236
## Authoring, testing, and contributing samples
3337

3438
See [AUTHORING_GUIDE.md](AUTHORING_GUIDE.md).

MAC_SETUP.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)