Matt Not Synonymous Cree Matt Not Synonymous Cree - 1 month ago 9
Linux Question

Running a Python program from the command line using only the program name

I'm being asked to make a python program that parses tokens.

The usage is as follows:

$ cat input.txt | program "some text %{0} and %{1S3}" > output.txt


but the best I can manage is:

$ cat input.txt | py program.py "some text %{0} and %{1S3}" > output.txt


or if I make the script executable, remove the file extension, and I'm in the current directory

$ cat input.txt | ./program "some text %{0} and %{1S3}" > output.txt


Is there a way for me to use the first example's style of execution with a python script? Ideally I'd also be able to use it from anywhere, not necessary when pointing at the directory containing the program.

Edit: I've tried this:

Here's what I tried --

$ cd projects/token_parser/
$ ln -s grap /usr/local/bin/grap
ln: failed to create symbolic link '/usr/local/bin/grap': Permission denied
$ sudo ln -s grap /usr/local/bin/grap
[sudo] password for fosssaintdross:
$ grap
bash: grap: command not found

Answer Source

You need to make sure the location containing program.py is in the PATH environment variable. Alternatively you can link to it from a path that is already in the list, for example:

ln -s /path/to/program.py /usr/local/bin/program