Assalamualaikum,
The official programming language for Raspberry Pi is Python. Python is a flexible programming language that runs on almost all platform. A program can be simply written on any kind of machine such as Windows or even a Mac and it can be loaded into your Raspberry Pi, and vice versa. To be honest to everyone, this is my first time studying Python. I am used to C and C++ programming before this when playing around with PIC.
There a lot of Book or eBook that you can refer and explore more about Python Programming with Raspberry Pi. Simply google and you will be given with the result. For my study, I use Sams Teach Yourself Python Programming for Raspberry in 24 Hours (Second Edition) as my first reference book. The author of this book are Richard Blum and Christine Bresnahan. You can it on Kindle or in Paperback from Amazon.
Before starting to write your own program or following samples in the book, you will need to setup your programming environment. Python nowadays comes with two version which is Python2 or Python3. For me, I prefer to choose Python3 as it is simply a newer version. Raspbian comes with Python3 and you don’t need to download it. Not to forget, it is also preloaded with the necessary tools for you to start with such as:
- Python interpreter
- Interactive Python shell
- Python development environment
- Text editors
Checking your Python version
- Simply click on Terminal icon located in the GUI. It can also be accessed remotely but you need to have a SSH client. For Windows user, you can download PuTTY and for Mac user, you can use built-in Terminal which is available in your Mac OS X. In my case, I am logging in remotely using built-in Terminal from my Mac machine.
- Login to your Raspberry Pi remotely using the assigned IP address. The default username is pi and password is raspberry. The command that I used is
ssh -l pi <Raspberry Pi IP Address>
. Example as follow: - At the shell command line, type in
python3 -V
and you should see your Python version installed. Example as follow: - If you get a message command not found, it could be your
PATH
environment is not properly set or Python3 is not installed.- By default, Python3 is available in
/usr/bin
folder. To verify yourPATH
environment, type inecho $PATH
and ensure that/usr/bin
is in the list. I am pretty sure that/usr/bin
will be in the list since this is the default folder for any Linux based binary files. However, if its not in the list, the easiest way to fix it is by typingPATH=$PATH:/usr/bin
. This fix is just a temporary fix for your current session. For a permanent fix, you will need to do some modification on your user profile. - There is high chance that the reason could be that Python3 is not installed in your Raspberry Pi. To fix this, you will need to install it. Ensure that you have an internet connection and type in
sudo apt-get install python3 idle3
. Let it run and follow the on-screen instruction. Now repeat Step 3 above and you should be all set!
- By default, Python3 is available in
Checking your Text Editor
There are lot of Text Editor software available for Raspbian. By default, Rapsbian comes with vi
and nano
. Both are great tools but for Raspbian, I am using nano as my Text Editor software. Now that you have learned how to access the shell command line, fire up your Terminal again to access your Raspbian shell command line and follow below guide:
- Type in
nano -V
to find out your nano version. Below is an example: - If you get a message command not found, you can refer my writing above on how to fix the
PATH
environment. Beside, it might be thatnano
is not installed by default. To install it, simply type insudo apt-get install nano
and follow the on-screen instruction and you are good to go.
The above should be able to get you started to write Python programming in Raspbian.
Ameer.