Preparing for the classes¶
Overview¶
The plan is to do this as a few 3 hours or so segments. Content will be mostly me droning on about the stuff in these notes with participants doing some live Postman, OAuth 2.0 testing using a deployed version of the demonstration app, followed by Python/Django coding on their laptops.
Some of the material here may be too basic or the opposite so it's hard to estimate how much time it will take. We may refer to some stuff and then skip ahead. Anything not done in class should be done as homework. We can always add on more sessions if we need to.
For CUIT staff, you can
always ask questions on Slack channel #python-dev
Expectations¶
- It is expected that students will bring laptops that have a functional Python development environment set up, including Python 3.5 or higher, Postman, git, an editor or IDE, maybe Docker for some of the advanced examples. The examples are all MacOS-based, using the Unix Terminal CLI. Windows users will need to adjust as needed (see SW Carpentry's shell less setup for Windows instructions).
- Optionally have PyCharm installed
and licensed (not required; PyCharm CE works fine too). Some use Microsoft Visual Studio.
Or use a programmer's editor like Emacs, vi or atom.
However, PyCharm's ability to interactively debug is a lot more powerful than
$ python -m pdb ...
. - git
- CLI tools installed
- access to gitlab/github
- have successfully created one's own personal repo.
- SW Carpentry's Git lesson is a good resource to get started with git.
- Some familiarity with: JSON, HTTP, REST, XML, SOAP/WSDL, SQL, git, pycharm, Python object-oriented programming, Django, etc. I don't really care if you've heard of XML, SOAP or WSDL other than if you have heard of them, but not JSON or REST, they'll be a point of comparison. Similarly, knowledge of SQL can be cursory, basically understanding the concepts of tables and fields (columns).
Development Environment Setup¶
Most all steps on macOS will be performed using the Unix shell. Open the "Terminal" application for this.
Homebrew¶
Only macOS is supported by Homebrew. Windows users can skip this section.
macOS¶
Homebrew is a package management system used to simplify software installations on macOS.
Run the command listed at https://brew.sh/
Python¶
macOS¶
Once Homebrew is installed, it should be simple install python:
1 |
|
More details are available at: http://docs.python-guide.org/en/latest/starting/install3/osx/
You can create an alias in your ~/.bashrc or ~/.bash_profile file to set python3 as default:
~/.bashrc
1 |
|
See more about running "multiple versions of Python for use with tox on macOS", below, for more advanced Python tricks.
Windows¶
See https://www.python.org/downloads/windows/
This should install Python3 in C:\\Program Files\\Python36
Git¶
macOS¶
The Git installation for macOS is easily done with Homebrew:
1 |
|
If you would prefer to install this manually without homebrew, see: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
Windows¶
- Download git from https://git-scm.com/download/win
- Click 'Run as administrator' on the .exe file in your Downloads directory.
- Set the installation directory to the following path:
C:\\Program Files\\Git
- If there is an option to add git to your path, select it.
- Accept all defaults otherwise.
If git was not added to your path, add the above directory to your system’s Environment Variables
You may also want to take a look at Git for Windows.
PyCharm¶
PyCharm is an IDE for Python development. This instruction applies to both macOS and Windows.
Download the professional or community edition from the following link and follow the setup instructions: https://www.jetbrains.com/pycharm/download/
Postman¶
Postman is a powerful tool for testing HTTP. We'll be using it extensively to test our APIs. If you don't already have it, you can get it at: https://www.getpostman.com/.
Advanced Topic: Multiple Version of Python for use with Tox on macOS¶
Why do I need multiple Python versions?¶
The problem: typical tox.ini files test against multiple version of Python – py34, py35, py36 – so you need all those versions installed on macOS. For the purposes of this material, you can get by with the system version as described above.
Installing pyenv¶
Install pyenv:
1 |
|
Add eval "$(pyenv init -)"
to your bash profile (or similar for other shells):
1 2 |
|
Don’t forget to do the eval once if you have an open shell window.
Installing multiple Python versions with pyenv¶
Then choose versions of python you want. pyvenv install -l
lists what’s available. There are many flavors and versions. We just care about 3.[456] right now:
1 2 3 4 5 6 7 8 9 10 |
|
Install those versions and make them “global”¶
1 2 3 4 |
|
Installation under Mojave and XCode 10¶
See this issue where pyenv install complains about missing zlib. Here’s a workaround:
1 2 3 4 |
|
Choosing which Python version to use¶
The preceding will make python3.6, python3.5 and python3.4 globally available. The first-listed version is the one that becomes “python”:
1 2 |
|
pyenv global
makes the versions global (for you only; it updates ~/.pyenv/version)
pyenv local
makes the versions local to the CWD (it updates .python-version). Local settings override global.
See https://github.com/pyenv/pyenv/blob/master/COMMANDS.md for more.
Install the tox-pyenv plugin¶
This plugin makes tox magically find the versions required in the tox.ini file.
1 |
|
For example, in a tox.ini that tests a matrix of multiple Python versions, it will make sure py34 uses python3.4, py35 uses python3.5 and so on.
Here’s a snippet from a Django add-on library that does a cross-product series of tests of three Python versions against django 2.0 and the django project’s current master branch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
If you’re running tox and you have all the interpreters installed already you can use tox -e
to choose the one version to run tox with. Otherwise, tox will run with py34, py35 and then py36.
1 |
|
Installing system python¶
You can still use homebrew to install the system python. Brew-installed system python is located in /usr/local/bin. macOS (High Sierra or older) also still installs py27 in /usr/bin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
You can also install a newer version of legacy Python 2.7:
1 |
|
This might be a bit confusing since the pyenv shim is still first in your path (thanks to eval "$(pyenv init -)"
):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|