The Raspberry Pi is a fully-fledged mini computer, capable of doing whatever you might do with a computer. It comes with 4x USB, HDMI, LAN, built-in Bluetooth/WiFi support, 1GB RAM, 1.2GHz quad-core ARM CPU, 40 GPIO (General Purpose Input Output) pins, audio and composite video output, and more. In this post, I’ll give you a quick overview of what a Python program is, what Python programs can be used for, and how to write and run a simple Python program on the Raspberry Pi. Python is a very useful programming language that has an easy to read syntax, and allows programmers to use.
Previously I wrote about. I followed up with a post that detailed, and now I'd like to share how you can do the same with a Raspberry Pi. Why the Pi?While it's possible to it has it's limitations.First, you can't truly automate running them. You can trigger them by time or location, but you will have to unlock your phone and hit OK on each trigger.Second, you can't take advantage of some of the more advanced modules. One example is, which can create virtual browser instances in the background, visit websites and take actions on them. This is not possible due to iOS's locked down architecture.On the Pi (Linux) you have no limits.
You can use any module and schedule scripts to run fully automatically, whenever you like. Ok, but the Pi is running Linux. Why not just buy and old laptop running Linux and use that instead?You can certainly do that, but I think the Pi is much more practical. It has the size of a bank card. If you put it in a case it's more like a PowerBank.
![Raspberry Raspberry](http://www.sparqee.com/wp-content/uploads/2015/02/20150205_SparqEE_schematic_shieldR_BP_v4.png)
If you're like me and use it as a hub that you connect to from various devices, then it's just much easier to find a place for it at home where it will not ruin the aesthetics. Also, it's much easier to take it with you while you travel.That being said, everything what I will describe here can be also done with a cheap laptop running Linux, if you fancy that. The first stepsThe very first step as you might guessed it is to buy a Pi. I recommend getting it with an SD card that has pre-installed on it.Also, be careful as the standard package does not contain a power adapter. You can use it with more powerful phone charger if you have any extra laying around.For the very first setup you will also need a display, keyboard and mouse.Once you started up the Pi for the first time, connect with it to your local network. This can be either cable or through Wifi.Then, start and configure (comes with Raspbian) as it is a really convenient way to access you Pi, even remotely. Configuration is super easy, just register an account and follow the Wizard.Once it's done, use VNC Viewer on any device to log in.
(, )After this has been done, you basically don't need to connect any peripherals to the Pi anymore. Raspbian will detect if you start it up without any display, keyboard or mouse connected, and it will automatically boot into a so called header mode.
In header mode, you can connect to the Pi through SSH or via RealVNC from any device: phone, tablet, laptop. And not just from your local network, but also remotely. Setting up the Pi to run Python scriptsAll right, your Pi is up and running, now you just need to configure it to run Python scripts.The good news is: there's not much to do if you are using Raspbian, as it has Python installed out of the boxOne thing you can do is download the latest version, probably because Raspbian does not have the latest release. Head over to, and upgrade if necessary.But other than this you are good to go. You can copy your existing.py files and start using them with the built in IDE or Terminal straight away.Speaking of which: if you are copying over files you used on Windows or OS X, remember that on Linux you'll need a different shebang line. You probably know where to look this up, but here it is for reference:. On Windows, the shebang line is #!
Python3. On OS X, the shebang line is #!
/usr/bin/env python3. On Linux, the shebang line is #!
/usr/bin/python3.(shebang lines are the first lines in python script files that tells the system where to locate the interpreter)Also, if you want to start scripts from the terminal there are a few extra steps to do:. Place them.py files in your home folder. Make them executable with the following command: chmod +x script.pyNow you can start a script by typing in./script.py to the terminal(If you place them somewhere else than your home folder you will have to describe the whole path to start them)Automating scripts on the Pi with CRONNow to the interesting part.
So far we set up our Pi and started to run python scripts on it. But logging in through RealVNC or SSH every time we want a script to run is not really convenient (with the exception if we want to trigger scripts via SSH coming from another device or service automatically, but this will be detailed in another post).If we have a web-scraping script that looks for some info on the web every day and returns some information (like the explained in my previous post), then what we want is total automation. The script should start all by it self, do it's job, and return the information.Luckily we can do that with a built in tool in Linux called CRON. How to setup CRON. Log into your Pi with realVNC, and start Terminal. In the Terminal, enter crontab -e. If you want a command to run with system permissions, use the sudo crontab -e command to open the root account’s crontab file.
You might be asked to select an editor.