This section will show you how to run your first Python program on a Linux computer.
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.
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! >>>
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. >>>
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
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.
Test your setup by running hello.py again.