Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
2  
This isn't specific to Ubuntu. You may be better off asking programming questions on stackoverflow.com –  Jay _silly_evarlast_ Wren Sep 14 '12 at 14:58
add comment

3 Answers

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

 #!/usr/bin/python

 print "Hello world"

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

 chmod a+x test.py

Presto, you can now run it:

 ./test.py

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 test.py /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 test.py $HOME/bin

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

test.py

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

susan

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
add comment

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 foo.py in your home directory, you can run that script with command

$ ~/foo.py

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 ~/foo.py

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

$ echo $PATH
/usr/local/bin:/usr/bin:/bin

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

$ sudo mv ~/foo.py /usr/local/bin
$ foo.py
share|improve this answer
1  
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 test.py will work, but ./test.py not. –  January Sep 14 '12 at 7:49
    
Thanks, I updated my answer accordingly. –  th0th Sep 24 '12 at 8:11
add comment

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/myapp.py "$@"

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

share|improve this answer
1  
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
add comment

Your Answer

 
discard

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.