how to make a python file run without py extension
Solution 1
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:~$
Solution 2
make a symbolic link
ln -s some_file.py some_file
now you can type your cmd like this:
some_file Steve Jobs
Related videos on Youtube
Shiva Krishna Bavandla
I love to work on python and django using jquery and ajax.
Updated on September 16, 2022Comments
-
Shiva Krishna Bavandla over 1 year
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 ?
-
Shiva Krishna Bavandla almost 11 yearsExactly 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""
-
user4815162342 almost 11 yearsThe 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. -
Shiva Krishna Bavandla almost 11 yearsAlso 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 that
-
NickUpson almost 11 yearschmod u+x some_file no space between 'u' and '+'
-
NickUpson almost 11 yearsto avoid the "./" you would need to add the directory containing the file to your search path $PATH
-
Shiva Krishna Bavandla almost 11 yearsk what i had done is created a .pth file in site directories and given the directory path in it, but still it snot working, so can u please point me in to the right direction ?
-
Shiva Krishna Bavandla almost 11 yearsoh its really wierd, i performed the same steps as above, but still its complaining as ""hw: command not found ""
-
BlackBear almost 11 years@shivakrishna does the link (i.e. /usr/bin/hw) exist?
-
Shiva Krishna Bavandla almost 11 yearsbut 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 work
-
Shiva Krishna Bavandla almost 11 yearsso if we place the file permanently in python path whether we able to access the file in the above manner ?
-
TangZ almost 11 yearsif u r using Linux, create a file
emacs -nw some_file
, add this at the beginning#!/bin/env python
, andchmod +x some_file
, run it! I just tried, it works. -
Ciasto piekarz over 10 yearsi think it doesn't work on bash shell, any idea how would that work on OS x in bash shell?
-
cjs over 5 yearsThe 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.