11

I had a small python script that takes input from the command line arguments and done some operations using the inputs taken and display the result

Below is the 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 am executing this as

python some_file.py Steve jobs 

Result :

Hello Steve Jobs !!!! 

Now what all i want is, i don't want to use python command before file name and extension of python file name, that is i want to run the file as a command tool as below

some_file Steve Jobs 

so what to do in order to run the python file as above ?

0

    3 Answers 3

    7

    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 the script. Run it with ./some_file parameters.

    If you want to launch it with some_file parameters simply make a link to the script in a directory which is already included into your PATH: sudo ln -s some_file /usr/bin.

    So, here's the full 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:~$ 
    8
    • Exactly i had added path at the start of the script and made it executable with chmod u +x. and after that when i run liek above it displayed ""bash: ./some_file: No such file or directory""CommentedJul 11, 2013 at 11:46
    • 1
      The shebang line must contain the full path to the Python interpreter, or a program of known path that will find it, such as #!/usr/bin/env python. Also, the shebang-based solutions will work only on Unix-like OS-es, not on Windows.CommentedJul 11, 2013 at 11:46
    • Also i dont want to use even "./" before some_file, i want to use it as a tool for example if type top in linux it will show some details right, i mean thatCommentedJul 11, 2013 at 11:48
    • chmod u+x some_file no space between 'u' and '+'
      – NickUpson
      CommentedJul 11, 2013 at 11:49
    • to avoid the "./" you would need to add the directory containing the file to your search path $PATH
      – NickUpson
      CommentedJul 11, 2013 at 11:49
    6

    make a symbolic link

    ln -s some_file.py some_file 

    now you can type your cmd like this:

    some_file Steve Jobs 
    5
    • but i think this is temporary solution, because if we copy the file at some other location then i hope we need to run the above command in order to run the file without py extension right ?. what i am searching and talking about is it doesn't wherever the py file exists, but when we run the file like some_file(without extension) it should workCommentedJul 15, 2013 at 7:53
    • so if we place the file permanently in python path whether we able to access the file in the above manner ?CommentedJul 15, 2013 at 7:56
    • if u r using Linux, create a file emacs -nw some_file, add this at the beginning #!/bin/env python, and chmod +x some_file, run it! I just tried, it works.
      – TangZ
      CommentedJul 20, 2013 at 1:37
    • i think it doesn't work on bash shell, any idea how would that work on OS x in bash shell?CommentedOct 13, 2013 at 20:31
    • The link works fine in Bash whether on Linux or MacOS. However, you may have to mark the original file as executable: chmod +x some_file.py. Also, if the link is not in a directory in your path you may have to specify a path to it, e.g., ./some_file if the link is in the current directory.
      – cjs
      CommentedSep 13, 2018 at 4:29
    -1

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

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.