Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write an Ubuntu application that helps me deal with a web-based API. I can write it in Python, but I want to know how to build it so that I can execute it as a command on the terminal itself.

share|improve this question
This isn't specific to Ubuntu. You may be better off asking programming questions on – Jay _silly_evarlast_ Wren Sep 14 '12 at 14:58

Put your Python program in a text file. The first line of that file should be


 print "Hello world"

(I put the "Hello world" line just as an example of Python code). Save it as a text file, for example Make that file executable

 chmod a+x

Presto, you can now run it:


To be able to access it from any directory and for every user, put it in /usr/local/bin, which is the default location for executables not installed with package managers:

sudo cp /usr/local/bin

Alternatively, you can make a directory called "bin" in your home directory; it will be automatically appended to your PATH variable from through the $HOME/.profile file.

mkdir $HOME/bin
mv $HOME/bin

In both of these two last cases to run your script in the terminal, you have to write only:

The extension ".py" is here only for you to indicate that it is a Python script. You can call it "susan" and run it by typing


It will work the same.

share|improve this answer
Thank you but I know that. I wanted to know how to make it executable like this: typing "test" on the terminal should print me hello world. – Puck Sep 14 '12 at 18:51
Huh, you can call it "test" or "susan" for what I care :-) In Linux, extensions are for your eyes only -- the system doesn't really care about them. Rename it to "test", move it to a folder from your PATH like /usr/local/bin or /home/user/bin and bob is your uncle. (but beware: there already is a program called "test") – January Sep 14 '12 at 23:08

Python is a scripting language, which means that you don't have to compile it before running. All you need is getting required version of Python interpreter installed.

For example, you have a python script named in your home directory, you can run that script with command

$ ~/

The first line in the file should tell it's a python file. First line should be like:

#!/usr/bin/env python

Assuming it's set as executable, if not:

$ chmod +x ~/

If what you want is to run it without any ~/ stuff, you should move it to your $PATH.

$ echo $PATH

If you move in /usr/local/bin, /usr/bin or /bin you can run without any parent folder.

$ sudo mv ~/ /usr/local/bin
share|improve this answer
This will not work unless you specify the interpreter in the Python file. This first line, '#!/usr/bin/python' is by no means required by the Python language, so if it is missing, python will work, but ./ not. – January Sep 14 '12 at 7:49
Thanks, I updated my answer accordingly. – th0th Sep 24 '12 at 8:11

As it works fine to put a small python script to /usr/local/bin to be able to run it from the command line (see this answer) we may have a more elaborate python application that we need to store in another place (e.g in /opt/myapp/).

We can then write a small script we put in /usr/local/bin/ that references to our python script like e.g.

#! /bin/bash

python /opt/myapp/ "$@"

Add the variable "$@" in case your script take command line parameters.

share|improve this answer
Edited your message so it now correctly reads #!. Other than that, what's the point of calling bash to call python when you can directly call python? – January Sep 14 '12 at 10:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.