简体   繁体   中英

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?

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

in linux-based OSs you must include a line (at the beginning of your script, ie, 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 (ie executable) for that to work.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM