I am new to python. I wrote a program which can be executed by typing python Filecount.py ${argument}
Why I see my teacher can run a program by only typing Filecount.py ${argument}
. How to achieve that?
3 Answers
Make it executable
chmod +x Filecount.py
and add a hashbang to the top of Filecount.py
which lets the os know that you want to use the python interpreter to execute the file.
#!/usr/bin/env python
Then run it like
./Filecount.py args
- 4
/usr/bin/env
is preferred to/bin/python
. There is not even a proper/bin
directory on my system (this would resolve through a symlink). Also, on certain Arch-based distros, this would resolve to Python 3, not Python 2, whereas I believe the/usr/bin/env
style resolves as expected.CommentedMar 25, 2014 at 16:02
in linux-based OSs you must include a line (at the beginning of your script, i.e., the first line) like this:
#!/usr/bin/python
this tells the OS to seek your python interpreter at that location. this applies to any script.
remember to have the permissions in your script file (i.e. executable) for that to work.
- As I pointed out on Andre's answer, the preferred shebang line is
/usr/bin/env python
or equivalent, rather than specifying the path where you suppose Python exists on the system.CommentedMar 25, 2014 at 16:03
Add a shebang line to the top of your file: http://en.wikipedia.org/wiki/Shebang_(Unix)#Purpose
It will tell the system which executable to use in running your program.
For example, add
#!/usr/bin/env python
as the first line, and then change the permissions of the file so you can execute it.
chmod +x Filecount.py
Best of luck!
Filecount.py
as opposed to./Filecount.py
that he has likely also added its directory to his PYTHONPATH.