How to run python script on terminal without using python3?

Viewed 3336

I have a problem: How can I run on terminal a script of python without using python3 before the name of script?

For example: For run my script I should write on terminal this:

$ python3 nameofthescript.py args

But I want that you to write this:

$ nameofthescript args

How can I do?

1 Answers

First, obviously rename the file

mv nameofthescript.py nameofthescript

Next, add this to the beginning of your file:

#!/usr/bin/env python3

Then make the file executable:

chmod +x nameofthescript

Now you should be able to run the file as:

./nameofthescript

If you want it to run from any directory, you have to move it to a directory inside your $PATH (usually /usr/local/bin):

mv nameofthescript /usr/local/bin
Related