So far to execute a Python program, I’m using
> python file.py
I want to run the Python script simply using file name, like
> file.py
similar to shell scripts like
> sh file.sh
> chmod +x file.sh
> ./file.sh
or move file.sh to bin and then run
> file.sh
Put this at the top of your Python script:
The #! part is called a shebang, and the
envcommand will simply locatepythonon your$PATHand execute the script through it. You could hard-code the path to the python interpreter, too, but calling/usr/bin/envis a little more flexible. (For instance, if you’re using virtualenv, that python interpreter will be found on your$PATH.)