Headless Raspberry Pi Zero W Setup with Bonus Python Program and Debugging!

Brian Gerson
4 min readSep 19, 2018
A headless human!

I just finished setting up my Raspberry Pi Zero W and it was both extremely easy and unexpectedly, annoyingly tricky. I’ll recap the easy part and record my debugging process for the tricky part here since I did not find all of information I needed in one place.

There are more robust headless setup tutorials out there, including this one that I initially used, but like I said — it may be helpful to include all of my steps here in case you run into the same issues.

If you just want to set up your Pi, feel free to follow along. If you’re here to debug shit, the issue I was solving was Python related, and looks like the following:

AttributeError: module ‘lib’ has no attribute ‘X509_up_ref’

I’ll cover what I did to encounter and fix that shortly.

Initial Setup

I had the Raspberry Pi Zero W from Adafruit with Headers, and a boot SD card with Debian Stretch Light pre-installed. If you need to flash it yourself, I recommend using Etcher and following along here, as I went down this path in my efforts to debug my issues.

If you want to get shit running and don’t have a keyboard/screen/mouse for your pi, we can make that work via SSH and your handy WiFi that’s included in the Zero W model.

First, you want to add some files to your SD card before booting up the pi. The following instructions are for Mac OS:

  1. Insert the SD card into your preferred machine (I was using Macbook Pro).
  2. Open up your terminal and cd /Volumes/ && ls. You should see a directory called boot if everything mounted correctly.
  3. cd boot
  4. touch ssh — this creates a blank file needed to enable SSH for your pi!
  5. vi wpa_supplicant.conf and add the following lines:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="YOUR WIFI NAME"
scan_ssid=1
psk="YOUR_WIFI_PASSWORD"
key_mgmt=WPA-PSK
}

It’s important you get your wifi info right! Only the ssid and psk need to be modified to reflect your network settings.

Ok, now :wq and get out of there. You’re ready to power up!

Booting Up

Insert your SD card into the pi, then plug in your 5V USB power. You should see some nice flashy green LCD lights on your pi. Now, it is time for fun!

Open up your terminal again. We’re going to SSH into your pi:

ssh pi@raspberrypi.local

You will be asked for a password. It is, unsurprisingly: raspberry

You’ll want to change that now that you’re in by typing passwd and updating it to something more secure.

(It sometimes takes a minute for your pi to boot, so if you don’t see a server after that long, maybe start troubleshooting that. I’m not going to include all that information here tho 😔)

Okay! You’re inside your raspberry pi! Now what?

You can run some demo programs (headlessly, you can really only run hello world) and learn about setting those up here. Otherwise, we can just get started with a simple python program that will send you a text. Why? Because this is ultimately part of a larger program I will be building, and it exposed a pretty annoying issue that I had to resolve that you may encounter as well.

First of all, you want to update and install some basics:

sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install python-pip

Kewl, now you have python. Let’s make that simple program. First things first: you’ll want to get a Twilio trial account. It’s pretty straightforward. Go ahead, I’ll wait.

Okay, NOW we’re ready.

First: sudo pip install twilio

Then we write 3 lines of code. You can use vi if you like, but I preferred to just use nano as I was experiencing some weird behavior with Vi on my pi.

nano funTextTest.py

You are now in a python file! You may wish to copy the following:

from twilio.rest import Clientclient = Client(YOUR_ACCOUNT_SSID, YOUR_AUTH_KEY)client.messages.create(to='+1your_number', from_='+1twilio_number', body='Hello my friend! I am a pi.')

That is it! Pretty straightforward, eh?? Now ^x to quit, Y to save, Enter to confirm.

To see if you’re about to have fun or not, run it: py funTextTest.py

If you got a text, cool: you are done. Goodbye!

Otherwise…

The Troubles Begin

I immediately ran into an error, with the most relevant message in the stack trace being:

AttributeError: module ‘lib’ has no attribute ‘X509_up_ref’

If you see this, you are in luck! I mean, you have a bug, but because I will tell you how to fix it without diving into a billion SO answers and forum pleas.

You essentially have a versioning issue with some dependencies. The answer (found eventually on this thread) is simple:

sudo rm -rf /usr/local/lib/python2.7/dist-packages/OpenSSL/
sudo apt install --reinstall python-openssl

Run your test again, and you should no longer experience any unfortunate SSL-related issues.

This appeared to be relatively common as an issue for folks using python libraries, but there were a lot of answers that I had to try before finding one that worked. Hopefully this helps!

If you saw a different error, feel free to post it in the comments. I saw a couple other wonky things that I may be able to point you in the direction of solving, but you may have your own issue to solve.

If the above error WAS your problem, and this solution does not work, please comment. I did some other dumb things to try and solve it that did not appear to factor into the solution, but may in fact have played some part in it finally working. I can include that information if it appears to be relevant!

--

--

Brian Gerson

Solver of problems. Brewer of beers. Befriender of dogs. Bike tourist and giant baseball nerd. Driven human being.