1

I have a command line app that I want to deploy. The only problem is that to run it you have to run the python file like this: `

$python application.py <arguement>

`. I have seen other applications made in python that can run like this but would require this input

$application<arguement>

How can I make this possible?

Thanks in advance

1 Answers1

1

Assuming you are doing it in bash (Linux) or similar, then have the first line of your source (.py) file as

#!/usr/bin/python

(depending on the location of your Python interpreter) or possibly

#!/usr/bin/env python

Then make the script executable

$ chmod +x foo

and make sure the directory is in your $PATH or explicitly specify the directory in your command e.g.

./foo
TimGJ
  • 1,584
  • 2
  • 16
  • 32
  • OP is not asking about running python script in windows or linux and making the script as executable. – danglingpointer Apr 25 '17 at 22:01
  • This is exactly what I want but what do you mean make it executable? –  Apr 25 '17 at 22:09
  • Executable files (programs) in Linux have a special flag (x) to dstinguish them. The chmod +x sets this flag so the system will attempt to execute the program if you ask it to. I suggest you go and read a tutorial on the Linux file system and how file permissions work. – TimGJ Apr 25 '17 at 22:12
  • Ok so it worked but I have to do ./.py. Is there a way to only have to do ? Also, I can only use it in the directory where my file is. Is there a way to make it usable everywhere? –  Apr 25 '17 at 22:47
  • Assuming you are using Linx/bash, make sure that the directory in which the Python scripts is on your $PATH (https://unix.stackexchange.com/questions/26047/how-to-correctly-add-a-path-to-path). That way, assuming there is nothing with the same name before it on your $PATH it should execute for you anywhere. – TimGJ Apr 26 '17 at 05:31
  • Also, you don't need the .py extension at the end of the file. (Note, though, that a lot of editors wont recognise your program as Python source code if you do and so won't highlight keywords etc). – TimGJ Apr 26 '17 at 05:33
  • Ok so it worked but when I quit the terminal and open it again I need to run: export PATH=$PATH":$HOME/bin" again. How can I make it permanent? –  Apr 26 '17 at 16:54
  • http://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux – TimGJ Apr 27 '17 at 07:14