How to make python file without py extension

I had a small python script that enters data from command line arguments and does some operations using the inputs I entered and displaying the result

Below is a working example code

some_file.py

import sys arguments = sys.argv first_name = sys.argv[1] second_name = sys.argv[2] print "Hello {0} {1} !!!!".format(first_name,second_name) 

Now I do it like

 python some_file.py Steve jobs 

Result:

 Hello Steve Jobs !!!! 

Now, what I want, I do not want to use the python command before the file name and the python file name extension, i.e. I want to run the file as a command tool, as shown below

 some_file Steve Jobs 

what to do to run a python file as above?

+6
source share
3 answers

Unix-like OS solution: the first line of the file should be #!/usr/bin/python (or wherever the python interpreter is), and chmod u+x script. Run it with ./some_file parameters .

If you want to run it using some_file parameters , just create a link to the script in a directory that is already included in your PATH: sudo ln -s some_file /usr/bin .

So here is the complete procedure:

 blackbear@blackbear-laptop :~$ cat > hw #!/usr/bin/python print "Hello World!" blackbear@blackbear-laptop :~$ chmod u+x hw blackbear@blackbear-laptop :~$ sudo ln -s hw /usr/bin blackbear@blackbear-laptop :~$ hw Hello World! blackbear@blackbear-laptop :~$ 
+3
source

make a soft link

 ln -s some_file.py some_file 

Now you can enter your cmd as follows:

 some_file Steve Jobs 
+3
source

you can run the same program in python shell with execfile('path') .

0
source

Source: https://habr.com/ru/post/949217/


All Articles