Python Hello World Example: How To Write and Execute Python Program on Unix OS

by Ramesh Natarajan on September 18, 2009

Question: I would like to understand the basics of how to write and execute a python program on Linux OS. Can you explain it with a simple example?

Answer: In this article, let us review very quickly how to write a basic Hello World python program and execute *.py program on Linux or Unix OS.

1. Write a Hello World Python Program

Create program as shown below.

$ vim


# Hello world python program

print "Hello World!";

2. Verify Python Interpreter Availability

Make sure python interpreter is installed on your system as shown below.

$ whereis python
python: /usr/bin/python /usr/bin/python2.5 /usr/bin/python2.6 /etc/python
/etc/python2.5 /etc/python2.6 /usr/lib/python2.4 /usr/lib/python3.0
/usr/lib/python2.5 /usr/lib/python2.6 /usr/local/lib/python2.5
/usr/local/lib/python2.6 /usr/include/python2.5 /usr/include/python2.6
/usr/share/python /usr/share/man/man1/python.1.gz

$ which python

3. Execute Python Program

You can either execute using “python” or “./”.

$ python
Hello World!

( or )

$ chmod u+x

$ ./
Hello World!

Note: As python is an interpreted language, you don’t have the compilation step similar to the C program.

4. Python one liner

You can also execute python from the command line as shown below. This will print Hello World!.

$ python -c 'print "Hello World!"'

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 13 comments… read them below or add one }

1 Richard February 1, 2010 at 6:50 am

Are you typing Step 1 into the terminal, or are you using a text-editor program (or similar program)? And if the latter, are you saving it, for example: “” ?
I appreciate your help.

2 Richard February 1, 2010 at 7:09 am

I decided to try the text editor approach.
Step one I saved as .
Step two in my terminal showed what it shows in step two here.
Step three in my terminal didn’t go so well:

a1@a1-desktop:~$ ./
bash: ./ No such file or directory

a1@a1-desktop:~$ python
python: can’t open file ‘’: [Errno 2] No such file or directory

a1@a1-desktop:~$ chmod u+x
chmod: cannot access `’: No such file or directory

This worked.
a1@a1-desktop:~$ python -c ‘print “hello world!”‘
hello world!

What have I not done correctly here?

3 David March 6, 2010 at 1:00 pm

It sounds like your file is not in your desktop directory. You need to be in the directory where your python file is located currently in order for the author’s instructions to work. In a terminal do an ‘ls’ without quotes. If you’re file is not listed, you’re probably not in the right directory (a.k.a. “folder”). You can use the cd .. command to the parent directory of your desktop, or cd to go to a subdirectory.

4 oj October 5, 2011 at 11:27 pm

it works fine I used nano editor

5 first rock September 13, 2012 at 6:01 am

What is the significance of “-c” in the last method?
Because without that we get an error that n such file or directory. is that native to Unix or Python..?

6 Pradeep s October 3, 2012 at 1:24 am

it may be for Compilation explisitly (-c)

7 SOLOMON October 8, 2012 at 11:25 am

>>>str2=( )
>>>print “HELLO WORLD.”;

can anybody help me on this code-where am i going wrong or its incorrect completely.

8 Mike Blaszczak February 21, 2013 at 4:07 pm

Users of Python 3.0 and newer will want to enclose the arguments to print in parenthesis, like this:

print( “Hello, world!” );

Note the parenthesis work fine in Python 2.x, as well — it’s just that Python 3.0 is strict about them by default.

9 simon June 15, 2013 at 11:37 am

Just installed raspbian into VMware and am trying to get started on programming in python.
I can type one liners into pygame.
I found abiword & leafpad i’ve type stuff in and saved it but how do you get it to run.
opening abiword in pygame shows everything including embedded code, leafpad show what i type in but doesn’t run.
I haven’t found anything that looks like cmd line.

How do you get code to run?

10 Divyapriya November 15, 2013 at 3:10 am

Thank you! This hepled me get started!

11 venki February 8, 2014 at 7:01 am

thank you .. I’m a beginner and learning python.. thanks for the help..

12 sarath March 3, 2014 at 2:24 pm

print( “Hello, world!” );
I saved I wanted to run the sme…one cmd prompt is flashing…am a beginner

13 kwek October 23, 2014 at 9:39 pm

How to run that hello world using website or desktop after running it in terminal?

Leave a Comment

Previous post:

Next post: