Friday 23 October 2020

PyBloom coding project part 2: Setting up the dev tools

In part 1 I introduced the PyBloom project, to indicated the outside temperature on Philips Hue lights. In part 2, I set up the development environment on my Mac.

Development Environment: Mac Mini

For a development environment, I decided I needed a pretty user interface, and something that would help me keep track of all my indents and nested brackets. This isn’t particularly taxing for any modern computer, but none of my computing options are particularly modern, so I needed something lightweight. So I chose my Mac Mini to be my dev machine.

Atom IDE

My machine is 10 years old, and a few macOS versions behind, so using heavyweight IDEs like Xcode is out of the question. Nor can I live without the pretty colours and error prompts without a nice IDE. I found Atom, built by the people who developed Git, and I like it lots. It’s hugely extensible with community-developed plugins, so I could create the combination for my needs. The ones I use the most turned out to be:

  • Atom-beautify

  • Atom-file-icons

  • Atom-python-virtualenv

  • Busy-signal

  • Intentions

  • Linter

  • Linter-flake8

  • Linter-ui-default

  • Minimap

  • Python-autopep8


I recommend you look them up, if you’re interested in building your own Atom configuration, that will run on even old and creaky machines.


Virtual environments and file structure

apt install virtualenvwrapper


There are plenty of reasons to work in a virtual environment, especially if your development machine is also your machine for everything else. I use a combination of virtualenv and virtualenvwrapper. Install using apt for all accounts, as it’s super useful. 


The wrapper is actually a collection of shell scripts, which simplifies the creation and management of virtual environments and whole coding projects. Once installed, it makes sense that you need to let your shell app (Terminal for most people using a Mac) where these scripts are. Insert the following configuration statements into your .bash_profile file.


export WORKON_HOME=$HOME/.virtualenvs

export PROJECT_HOME=$HOME/Projects

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

source /usr/local/bin/virtualenvwrapper.sh


Let’s go through each in order:

  1. This is the location of the virtual environment files, relative to the home directory. This is the top level folder, into which the project folder is created. These files include the specific instance of Python being used, instances of any installed libraries, and any config files. The . prefix indicates that the folder is hidden in the Finder app (unless an option is checked).

  2. This is the location of the project files. Project files are in a separate top level folder, distinct from the environment files. Examples of project files are the Python script, web page source files, config tokens. The project is kept separate from the environment so that you can roll back one without rolling back the other.

  3. There are commonly a few different versions of Python installed. This makes explicit that we’re only interested in v3.

  4. As we installed virtualenvwrapper for everyone, the shell script is located in the generic usr folder.


mkproject pybloom

workon pybloom


Once set up, it’s super easy to create and work in a virtual environment using the statements above. The first statement does three things: create a virtual environment in .virtualenvs called pybloom, and create a project folder in Projects also called pybloom, and links the two. The second statement activates the virtual environment, and changes the working directory to the pybloom project folder.


It’s worth taking a small detour into folder structures, as this gets quite complicated quite quickly. Unhelpfully, virtualenvwrapper and the Flask documentation suggest two slightly different folder structures, so what we end up with is a hybrid. We’ll need to be clear about this structure, as Flask is sensitive about where certain files are located. This is my recommendation - we’ll go through the content of each file as we progress through this project. 


./Project/pybloom

├── README.md

├── pybloom.py

├── utils.py

├── db.sql

├── docs/

├── app.py

├── app/

│   ├── __init__.py

│   ├── routes.py

│   ├── content.py

│   ├── templates/

│   │   ├── base.html

│   │   ├── feature1/

│   │   │   ├── feature1_page1.html

│   │   │   └── feature1_page2.html

│   │   └── feature2/

│   │       ├── feature2_page1.html

│   │       └── feature2_page2.html

│   └── static/

│       ├── image.svg

│       └── custom.css

├── tests/

│   ├── test_data.sql

│   └── test.py

├── requirements.txt

├── git/

├── .gitignore

└── MANIFEST.in

./.virtualenvs/pybloom

├── bin/

├── lib/

├── pyvenv.cfg

└── credentials.json

Installing the packages

I won’t spend too much time on describing the packages here, as it’ll make more sense to introduce them in context. If you're following along, go ahead and install all of them.


  • APScheduler

  • Flask

  • PyGal

  • PyOWM 

  • Qhue 

  • rgbxy


(Aside: Unlike virtualenvwrapper previously, we can’t install these using apt (which downloads from the Ubuntu repository); these need to be installed using pip (from the Python Software Foundation repository). This is a shame as apt has a useful apt update && apt upgrade method which enables all packages that it knows about to be upgraded at the same time. Pip doesn’t have this, so each package needs to be updated individually.)


pip freeze -> requirements.txt


Once we’ve installed all that we need, this command records the versions that we’ve installed in a config file. And that’s it for setting up the Mac Mini.



As we saw in this part, setting up an environment is quite involved. In part 3 I'll get into how I set up my second environment, the Raspberry Pi. Also visit https://github.com/Schmoiger/pybloom for the full story.

No comments:

Post a Comment

It's always great to hear what you think. Please leave a comment, and start a conversation!