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 helloworld.py program as shown below.
$ vim helloworld.py #!/usr/bin/python # 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 /usr/bin/python
3. Execute Python Program
You can either execute using “python helloworld.py” or “./helloworld.py”.
$ python helloworld.py Hello World! ( or ) $ chmod u+x helloworld.py $ ./helloworld.py 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!"'
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.Get the Linux Sysadmin Course Now!
If you enjoyed this article, you might also like..
|
|
|
|






My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 8 comments… read them below or add one }
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: “HelloWorld.py” ?
I appreciate your help.
-Richard
I decided to try the text editor approach.
Step one I saved as helloworld.py .
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:~$ ./helloworld.py
bash: ./helloworld.py: No such file or directory
a1@a1-desktop:~$ python helloworld.py
python: can’t open file ‘helloworld.py’: [Errno 2] No such file or directory
a1@a1-desktop:~$ chmod u+x helloworld.py
chmod: cannot access `helloworld.py’: No such file or directory
This worked.
a1@a1-desktop:~$ python -c ‘print “hello world!”‘
hello world!
What have I not done correctly here?
Richard,
It sounds like your helloworld.py 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 helloworld.py 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.
it works fine I used nano editor
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..?
it may be for Compilation explisitly (-c)
>>>str1=hello
>>>str2=( )
>>>str3=world
>>>print “HELLO WORLD.”;
can anybody help me on this code-where am i going wrong or its incorrect completely.
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.