Programming Environment: Linux

This section will show you how to run your first Python program on a Linux computer.

Python on Linux

Congratulations for being a Linux user, or for wanting to learn how to use Linux. Linux developers trust you to do powerful things with your computer, which means modifying your system to write the programs you care to write will be easier. In the Linux world, if something doesn't make sense, find a place to ask your questions. Someone has an answer, you just need to find the right people to answer your questions.

Installing Python

Python is probably already installed on your system. To find out if it is installed, open a terminal and type the word python. You will probably see output something like this:

$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

From this output, we can see that Python is installed, and the currently installed version is Python 2.7.3. You can start typing Python commands here, and you will see your output immediately:

>>> print("Hello Python world!")
Hello Python world!
>>> 

Installing Python 3

If you want to work with Python 3, you can do so fairly easily. Here is how you can get Python 3 up and running on Ubuntu 12.04:

Add the "deadsnakes" package archive (ppa) to your system. This archive has a number of older and newer versions of Python, including Python 3.3.

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python3.3

Now that Python 3.3 is installed on your system, you have two options. You can start a default Python 2.7 session by running the command 'python' in a terminal. You can start a Python 3.3 session by running the command 'python3.3' in a terminal.

$ python3.3
Python 3.3.2 (default, May 16 2013, 18:32:41) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

top

Installing Geany

Geany is a simple text editor, which makes it easy to run Python programs. Output is displayed in a separate terminal window, which gets you used to working in terminals as well.

  • Open a terminal, and install the package 'geany': sudo apt-get install geany
  • Press the windows button, and type 'geany'.
  • Drag the geany icon to the task bar on the left side of the screen. This creates a shortcut you can use to start geany.
  • Write a Hello World program, and save it as 'hello.py'.
  • There are three ways you can run a program in Geany:
    • Build > Execute
    • Press F5
    • Click the icon with three gears on it
  • You should see a terminal window pop up, with your output in it:
Hello Python world!


------------------
(program exited with code: 0)
Press return to continue

Configuring Geany to use Python 3

If you have installed Python 3 as described above, you may have to configure Geany to use Python 3.

Open Geany, and open a Python Hello World program. If you don't have one on your system, write one and save it as hello.py, and run the program. This makes sure that Geany is trying to run Python programs. When you have a running hello.py program, go to Build >> Set Build Commands.

Under 'Python commands', look for the 'Compile' line. Enter the following in the 'Command' box. Make sure you get the spaces right. You should have 'python3.3' followed by a space, and the rest of the command. If you have 'python 3.3', with a space between python and 3.3, Geany will not be able to run your code.

python3.3 -m py_compile "%f"

Under 'Execute commands', look for the 'Execute' line. Enter the following in the 'Command' box, paying attention once again to the spaces.

python3.3 "%f"

Test your setup by running hello.py again.

top