Updated on November 10, 2022
By Veronica Henry

Introduction to Python in Ubuntu Linux

Everything you need to get started with Python in Ubuntu including installing Python on Linux, running a Python script, and additional resources.

According to the TIOBE Index, Python is the most popular programming language today. It was created by Guido von Rossum in 1990, named after - you guessed it - the comedy, "Monty Python's Flying Circus". Like Java, once written, programs can be run on any operating system.

I began programming many years ago, and admittedly once I got into networking, turned away from my first love. But for those of you who would like to begin trying your hand at a new language, Python is excellent to start with. Supporters tout its low learning curve and elegant coding system.

Installing Python in Ubuntu

Ubuntu makes starting easy, as it comes with a command line version pre-installed. In fact, the Ubuntu community develops many of its scripts and tools under Python.

To make sure Python is installed, access your terminal using Ctrl + Alt + T and type "python3." If you see a "command not found" error, you can install Python with the Advanced Package Tracker by typing "sudo apt install python3."

You can begin programming in Python through either the command line version or the graphical Interactive Development Environment (IDLE).

How to run a Python script in Linux from the command line

Open a terminal window and type 'python' (without the quotes). This opens python in interactive mode.

While this mode is good for initial learning, you may prefer to use a text editor (like Gedit, Vim or Emacs) to write your code. As long as you save it with the .py extension, it can be executed in the terminal window.

Ready to write your first program? Many a road to programming excellence began with the old standby – the Hello World program. So that's where we'll begin.

At the prompt, type in print "Hello World!" (with the quotes) and hit enter. You'll see the command printed on the next line.

To run a script from the command line, type: python my_script.py. To exit the command line, type exit() or hit ctrl+d.

How to run a Python script in Linux from IDLE

Can't imagine writing a long program from the command line? Me either. In that case, you can try IDLE. Open a terminal window and type: 'idle'. (without the quotes). You'll see the python graphical shell loaded.

To write a Python script, click on File > New Window. This will open a sort of Notepad type editor. Type in the following code:

#linuxversion.py 
#Have user input version and print response
name = raw_input("What Linux release do you use?")
print "I also like", name, " - Linux rules!"

A couple things are going on here. The first 2 lines, preceded by the # sign are just comments. One of my earliest programming lessons was to always comment your work, not only for your own sanity, but for those who may need to edit the code after you.

The third statement uses the input gathered from the raw_input function and assigns it to a variable, name. Finally, the print statement will return the result.

Save the file, with a .py extension, then click on Run > Run Module from the menu to run the program.

Python Styles

If you’ve used other programming languages before, or even if you haven’t, you might want to familiarize yourself with Python’s Style Guide. You’ll want to be aware of Python’s styling idiosyncrasies, such as the importance of indentation and whitespace.

Resources for programming Python in Linux

Now that you have Python up and running, you'll undoubtedly want to move to on to a bit more complex programs. One of the best resources is the main Python website. Here are a few additional recommendations:

I hope you enjoyed this brief glimpse into Python. This is a language supported by a wide community and used by Google, NASA, Ubuntu and others. Give it a try for your next development project. Happy coding!

Veronica Henry

Veronica Henry is a writer, web developer and tech guru. Her 20 year IT career came to an end when her inner writer and entrepreneur inexplicably besieged her to give it all up. She is a self-proclaimed girl-geek and linux convert, who has held MCSE, GSEC and PMP certifications. In her dreams, she is a international best-selling sci-fi and fantasy author, but in the meantime, she now spends her days writing, managing her websites and wrecking havoc on her Ubuntu laptop.