Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 878
hidemyname Avatar asked Mar 25 '14 15:03

hidemyname


People also ask

How do I run a Python code in Shell?

To run the Python Shell, open the command prompt or power shell on Windows and terminal window on mac, write python and press enter. A Python Prompt comprising of three greater-than symbols >>> appears, as shown below. Now, you can enter a single statement and get the result.

Can you run a Python program without Python?

py2exe is a Python extension which converts Python scripts (. py) into Microsoft Windows executables (.exe). These executables can run on a system without Python installed. It is the most common tool for doing so.


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
like image 141
André Laszlo Avatar answered Oct 16 '22 20:10

André Laszlo


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.

like image 21
Luis Masuelli Avatar answered Oct 16 '22 20:10

Luis Masuelli


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!

like image 2
mattwise Avatar answered Oct 16 '22 21:10

mattwise