This section will show you how to run your first Python program on a Linux computer.
Python on Linux is pretty straightforward, because the people who develop Linux want you to write your own programs.
Python is probably already installed on your system. To find out if it is installed, open a terminal and type the word
python. You'll probably see output that looks something like this:
$ python Python 2.7.6 (default, Mar 22 2014, 22:59:38) [GCC 4.8.2] 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.6. You're now running a Python terminal session. You can start typing Python commands here, and you'll see your output immediately:
>>> print("Hello Python world!") Hello Python world! >>>
To exit the Python session and return to a terminal prompt, enter Control-D.
Python 3 is also installed on many newer Linux distributions. To see if you also have Python 3, enter
python3 at a terminal prompt:
$ python3 Python 3.4.0 (default, Apr 11 2014, 13:05:18) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
If your system doesn't have Python 3 installed, you can install it yourself. Here's how you can get Python 3 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.4.
sudo apt-get install python-software-properties sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update sudo apt-get install python3.4
Now that Python 3.4 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.4' in a terminal.
$ python3.4 Python 3.4.0 (default, Apr 11 2014, 13:05:18) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
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.
sudo apt-get install geany
Hello Python world! ------------------ (program exited with code: 0) Press return to continue
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 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' followed by a space, and the rest of the command. If you have 'python 3', with a space between python and 3, Geany will not be able to run your code.
python3 -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.
Test your setup by running hello.py again.