Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn an application or script into a shell command

When I want to run my python applications from commandline (under ubuntu) I have to be in the directory where is the source code app.py and run the application with command

python app.py

How can I make it (how is it conventionally done) to run the application from arbitrary directory with the command: app ? Similarly as you type ls, mkdir and other commands?

thank you

like image 264
xralf Avatar asked May 28 '11 17:05

xralf


4 Answers

  1. Add a shebang line at the beginning of your file:

    #!/usr/bin/env python
    
  2. Make your file executable by calling

    chmod +x app.py
    

    in the shell.

  3. Move it to some location included in the PATH environment variable and rename it to app. Alternatively, add the path of the directory containing app to the PATH environment variable by adding the line

    export PATH=$PATH:/path/to/app
    

    to your .bash_profile.

like image 148
Sven Marnach Avatar answered Nov 14 '22 23:11

Sven Marnach


Add the directory that the script is in to your path, make it executable, and add a proper shebang line.

In your .bashrc:

PATH=$PATH:/dir/to/the/script

Executable:

chmod +x myscript.py

At the top of the script, add the shebang line:

#!/usr/bin/env python

Then, from anywhere, you can just do:

myscript.py

(Note that you don't need a .py suffix, it could be called anything, e.g. app if you have a proper shebang line).

like image 20
Rafe Kettler Avatar answered Nov 14 '22 22:11

Rafe Kettler


  1. Add a shebang: as the top line of the file: #!/usr/bin/python or #!/usr/bin/python3 (you can use the python -B to prevent generation of .pyc files, which is why I don't use /usr/bin/env)

  2. Make it executable: You will need to do chmod +x app.py

  3. (optional) Add directory to path, so can call it anywhere: Add a directory with your executable to your $PATH environment variable. How you do so depends on your shell, but is either export PATH=$PATH:/home/you/some/path/to/myscripts (e.g. Linux distros which use bash) or setenv PATH $PATH:/home/you/some/path/to/myscripts (e.g. tcsh like in Mac OS X). You will want to put this, for example, in your .bashrc or whatever startup script you have, or else you will have to repeat this step every time you log in.

app.py will need to be in the myscripts (or whatever you name it) folder. You don't even need to call it app.py, but you can just rename it app.

If you wish to skip step #3, you can still do ./app to run it if you are in the same directory.

like image 34
ninjagecko Avatar answered Nov 14 '22 23:11

ninjagecko


Probably you want to symlink to your file location instead of adding another location to the path

chmod +x app.py
ln ~app.py /opt/local/bin/app

...assuming that /opt/local/bin is already in your path,.

Also, do not forget to add the shebang line to the first line of your script: #!/usr/bin/env python

like image 36
sorin Avatar answered Nov 14 '22 21:11

sorin