How to Install and Setup Python on Onion Omega 2

Updated 13 January 2018

In this installment of our Onion Omega2 tutorial series, we will cover the basic steps necessary to install and run python on the Omega2 or Omega2+.

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.

Putty open dialog

Mac and Linux users, open Terminal and enter:

ssh [email protected]

Remember, the default login is:

Username: root

Password: onioneer

Once you have logged it enter the following command:

opkg update

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, opkg and pip. It's usually faster to use opkg but 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:

opkg install pyOnionGpio

To install pip, use:

opkg install python-pip

Once 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 pip:

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:

mkdir /root/python
cd /root/python

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

Now use nano to create a new file named hello_world.py:

nano hello_world.py

Add the following lines to the empty file:

#Basic hello world python program.
print 'Hello World!'

Now <CTRL-X> then <Y> to save and exit.

Now we can run the hello world program with:

python hello_world.py

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.

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.