hidemyname
hidemyname

Reputation: 4287

How to run a python program in a shell without typing "python"

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?

Upvotes: 6

Views: 1615

Answers (3)

André Laszlo
André Laszlo

Reputation: 15537

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

Upvotes: 11

mattwise
mattwise

Reputation: 1506

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!

Upvotes: 2

Luis Masuelli
Luis Masuelli

Reputation: 12333

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.

Upvotes: 2

Related Questions