We'll cover how to install python or python3, how to install python modules and finally, we'll write a basic "Hello World" program. We'll also install the Onion Omega2 python library that will allow us to control the Omega's GPIO pins.
To install python we need access to the Omega's terminal shell, there are three ways to do this:
- Using the Omega's Serial port, by way of a USB <> Serial adapter, such as the Expansion Dock or Mini Dock
- Using the Virtual-Terminal in the Console-GUI webpage over WiFi.
- Connecting via SSH over WiFi
The Serial Port is the most foolproof method, but as not everyone has the necessary hardware, and the Console GUI is still quite buggy, this guide will use SSH over WiFi. However, the procedure is identical for all three methods.
If you are using Windows, you'll need to download and install PuTTY from here. If you're using MacOS or Linux then you already have everything you need.
Once your Omega is plugged in, powered on and booted up, start an SSH session:
Windows users, open putty, select SSH and enter your Omega's (omega-xxxx.local) address in the address field.
Mac and Linux users, open Terminal and enter:
Remember, the default login is:
Once you have logged it enter the following command:
There are two versions of python available Python2.7, normally just known as Python, And Python3. Both are easily installed and used, I won't go into the differences in this article, for more information see here. There is also a light package of both versions. If you require a lot of space for your project, you may want to use the light version to save on the Omega's limited space. However, for this tutorial, I will be using python2.7 full.
To install python2.7, execute the following:
opkg install python
To install python2.7 light, execute:
opkg install python-light
To install python3, execute:
opkg install python3
To install python3 light, execute:
opkg install python3-light
Now we have python installed there's one more thing we should cover before we move on to coding, modules. Most python projects are going to require some imported modules, so it's important we know how to install them. There are two ways to install extra modules,
pip. It's usually faster to use
pip has far more modules available.
To view the packages available via
opkg enter the following:
opkg list | grep python
To view only the packages specific to python3 use:
opkg list | grep python3
To install a package run:
opkg install <PACKAGE_NAME>
Since we will likely want to make use of the Omega's GPIO pins, we need to install the
pyOnionGpio package. Do so using
opkg install pyOnionGpio
opkg install python-pip
pip is installed, install modules with:
pip install <MODULE_NAME>
For example, to install the simple_math module, you would use:
pip install simple-math
If you come across this kind of error when using
root@Omega-296A:~# pip install paho-mqtt Collecting paho-mqtt Downloading paho-mqtt-1.2.tar.gz (49kB) 100% |████████████████████████████████| 51kB 215kB/s Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-cHKrSf/paho-mqtt/
Run the following command to fix it:
pip install --upgrade setuptools
Now that we have python successfully installed we can go ahead and write our first hello world program!
Create a folder
"/root/python/" to store our python programs:
If you're using the Console GUI then the editor app is probably the easiest way for you to edit your python program, if you're just using the shell, run the following command to install the
nano text editor:
opkg install nano
nano to create a new file named
Add the following lines to the empty file:
#Basic hello world python program.
print 'Hello World!'
CTRL-X> then <
Y> to save and exit.
Now we can run the hello world program with:
If all has gone according to plan you should see "Hello World!" printed on the screen. Congratulations you've just written your first python program for the Onion Omega! If you have any comments, questions or issues please leave a comment below or head on over to our forum, our full-time team of makers is just itching to help.