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?
Asked
Active
Viewed 1,567 times
6

Paulo Bu
- 29,294
- 6
- 74
- 73

hidemyname
- 3,791
- 7
- 27
- 41
-
4What operating system do you use? – Arjen Dijkstra Mar 25 '14 at 15:59
-
2If Windows: http://stackoverflow.com/q/1934675/1726343 – Asad Saeeduddin Mar 25 '14 at 16:00
-
It's worth noting that if your instructor is really using `Filecount.py` as opposed to `./Filecount.py` that he has likely also [added its directory to his PYTHONPATH](http://stackoverflow.com/questions/3402168/permanently-add-a-directory-to-pythonpath). – Two-Bit Alchemist Mar 25 '14 at 16:06
3 Answers
11
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

André Laszlo
- 15,169
- 3
- 63
- 81
-
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. – Two-Bit Alchemist Mar 25 '14 at 16:02
2
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.

Luis Masuelli
- 12,079
- 10
- 49
- 87
-
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. – Two-Bit Alchemist Mar 25 '14 at 16:03
2
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!

mattwise
- 1,464
- 1
- 10
- 20