Getting Started - Programming the Raspberry Pi: Getting Started with Python (2013)

Programming the Raspberry Pi: Getting Started with Python (2013)

2. Getting Started

The Raspberry Pi uses Linux as its operating system. This chapter introduces Linux and shows you how to use the desktop and command line.

image

Linux

Linux is an open source operating system. This software has been written as a community project for those looking for an alternative to the duopoly of Microsoft Windows and Apple OS X. It is a fully featured operating system based on the same solid UNIX concepts that arose in the early days of computing. It has a loyal and helpful following and has matured into an operating system that is powerful and easy to use.

Although the operating system is called Linux, various Linux distributions (or distros) have been produced. These involve the same basic operating system, but are packaged with different bundles of applications or different windowing systems. Although many distros are available, the one recommended by the Raspberry Pi foundation is called Raspbian Wheezy.

If you are only used to some flavor of Microsoft Windows, expect to experience some frustration as you get used to a new operating system. Things work a little differently in Linux. Almost anything you may want to change about Linux can be changed. The system is open and completely under your control. However, as they say in Spiderman, with great power comes great responsibility. This means that if you are not careful, you could end up breaking your operating system.

image

The Desktop

At the end of Chapter 1, we had just booted up our Raspberry Pi, logged in, and started up the windowing system. Figure 2-1 serves to remind you of what the Raspberry Pi desktop looks like.

image

Figure 2-1 Raspberry Pi desktop

If you are a user of Windows or Mac computers, you will be familiar with the concept of a desktop as a folder within the file system that acts as a sort of background to everything you do on the computer.

Along the left side of the desktop, you see some icons that launch applications. Clicking the left-most icon on the bar at the bottom of the screen will show us some of the applications and tools installed on the Raspberry Pi (rather like the Start menu in Microsoft Windows). We are going to start with the File Manager, which can be found under the Accessories.

The File Manager is just like the File Explorer in Windows or the Finder on a Mac. It allows you to explore the file system, copy and move files, as well as launch files that are executable (applications).

When it starts, the File Manager shows you the contents of your home directory. You may remember that when you logged in, you gave your login name as pi. The root to your home directory will be /home/pi. Note that like Mac’s OS X, Linux uses slash (/) characters to separate the parts of a directory name. Therefore, / is called the root directory and /home/ is a directory that contains other directories, one for each user. Our Raspberry Pi is just going to have one user (called pi), so this directory will only ever contain a directory called pi. The current directory is shown in the address bar at the top, and you can type directly into it to change the directory being viewed, or you can use the navigation bar at the side. The contents of the directory /home/ pi include just the directories Desktop and python_games.

Double-clicking Desktop will open the Desktop directory, but this is not of much interest because it just contains the shortcuts on the left side of the desktop. If you open python_games, you will see some games you can try out, as shown in Figure 2-2.

image

Figure 2-2 The contents of python_games, as shown in File Manager

You shouldn’t often need to use any of the file system outside of your home directory. You should keep all documents, music files, and so on, housed within directories on your home folder or on an external USB flash drive.

image

The Internet

If you have a home hub and can normally plug in any Internet device using an Ethernet cable, you should have no problem getting your Raspberry Pi online. Your home hub should automatically assign the Raspberry Pi an IP address and allow it to connect to the network.

The Raspberry Pi comes with a web browser called Midori, which you will find under the Internet section of your start menu. You can check that your connection is okay by starting Midori and connecting to a website of your choice, as shown in Figure 2-3.

image

Figure 2-3 The Midori web browser

image

The Command Line

If you are a Windows or Mac user, you may have never used the command line. If you are a Linux user, on the other hand, you almost certainly will have done so. In fact, if you are a Linux user, then about now you will have realized that you probably don’t need this chapter because it’s all a bit basic for you.

Although it is possible to use a Linux system completely via the graphical interface, in general you will need to type commands into the command line. You do this to install new applications and to configure the Raspberry Pi.

From the launcher button (bottom left), open the LXTerminal, which is shown in Figure 2-4.

image

Figure 2-4 The LXTerminal command line

Navigating with the Terminal

You will find yourself using three commands a lot when you are using the command line. The first command is pwd, which is short for print working directory and shows you which directory you are currently in. Therefore, after the $ sign in the terminal window, type pwd and press RETURN, as shown in Figure 2-5.

image

Figure 2-5 The pwd command

As you can see, we are currently in /home/pi. Rather than provide a screen shot for everything we are going to type into the terminal, I will use the convention that anything I want you to type will be prefixed with a $ sign, like this:

image

Anything you should see as a response will not have $ in front of it. Therefore, the whole process of running the pwd command would look something like this:

image

The next common command we are going to discuss is ls, which is short for list and shows us a list of the files and directories within the working directory. Try the following:

image

This tells us that the only thing in /home/pi is the directory Desktop.

The final command we are going to cover for navigating around is cd (which stands for change directory). This command changes the current working directory. It can change the directory relative either to the old working directory or to a completely different directory if you specify the whole directory, starting with /. So, for example, the following command will change the current working directory to /home/pi/Desktop:

image

You could do the same thing by typing this:

image

Note that when entering a directory or filename, you do not have to type all of it. Instead, at any time after you have typed some of the name, you can press the TAB key. If the filename is unique at that point, it will be automatically completed for you.

sudo

Another command that you will probably use a lot is sudo (for super-user do). This runs whatever command you type after it as if you were a super-user. You might be wondering why, as the sole user of this computer, you are not automatically a super-user. The answer is that, by default, your regular user account (username: pi, password: raspberry) does not have privileges that, say, allow you to go to some vital part of the operating system and start deleting files. Instead, to cause such mayhem, you have to prefix those commands with sudo. This just adds a bit of protection against accidents.

For the commands we have discussed so far, you will not need to prefix them with sudo. However, just for interest, try typing the following:

image

This will work the same way ls on its own works; you are still in the same working directory. The only difference is that you will be asked for your password the first time you use sudo.

image

Applications

The Raspbian Wheezy distribution for Raspberry Pi is fairly sparse. However, loads of applications can be installed. Installing new applications requires the command line again. The command apt-get is used to both install and uninstall applications. Because installing an application often requires super-user privileges, you should prefix apt-get commands with sudo.

The command apt-get uses a database of available packages that is updated over the Internet, so the first apt-get command you should use is sudo apt-get update

image

which updates the database of packages. You will need to be connected to the Internet for it to work.

To install a particular package, all you need to know is the package manager name for it. For example, to install the Abiword word processor application, all you need to type is the following:

image

It will take a while for everything that is needed to be downloaded and installed, but at the end of the process you will find that you have a new folder in your start menu called Office that contains the application Abiword (see Figure 2-6).

image

Figure 2-6 Abiword screen

You will notice that the text document in Abiword is actually part of this chapter. In fact, it is close to this part of this chapter, as I am writing it. (I can feel myself falling into a recursive hole. I may well vanish in a puff of logic.)

Abiword is a perfectly serviceable word processor. If I didn’t love my Mac quite so much, I would be tempted to write this entire book on my Raspberry Pi.

While we are on the subject of office applications, the spreadsheet stable mate of Abiword is called Gnumeric. To install it, here is all you need to type:

image

Once this application is installed, another option will have appeared in your Office menu—this one for Gnumeric.

To find out about other packages you might want to install, look for recommendations on the Internet, especially on the Raspberry Pi forum (www.raspberrypi.org/phpBB3). You can also browse the list of packages available for Raspbian Wheezy at http://packages.debian.org/stable/.

Not all of these packages will work, because the Raspberry Pi does not have vast amounts of memory and storage available to it; however, many will.

If you want to remove a package, use the following command:

image

This removes both the package and any packages it depends on that are not used by something else that still needs them. Be sure to keep an eye on the bottom-right corner of your File Manager window; it will tell you how much free space is available.

image

Internet Resources

Aside from the business of programming the Raspberry Pi, you now have a functioning computer that you are probably keen to explore. To help you with this, many useful Internet sites are available where you can obtain advice and recommendations for getting the most out of your Raspberry Pi.

Table 2-1 lists some of the more useful sites relating to the Raspberry Pi. Your search engine will happily show you many more.

image

Table 2-1 Internet Resources for the Raspberry Pi

image

Summary

Now that we have everything set up and ready to go on our Raspberry Pi, it is time to start programming in Python.