we help you to understand Python better

This article shows you how to create a Python script and execute it from a Unix-like shell without having to type python in front of it.

Executing a python script as a standalone app without typing python in front of it

Create a Python script and save as ‘hello.py’.

print("Hello World!")

This article is tested on macOS that has Python 2 and Python 3 installed. This article uses Python 3 to run the script with the following command:

python3 hello.py

Output:

Hello World!

Make the script executable as a standalone app

Step 1: Add Shebang

Add a shebang to the script. A shebang consists of the two characters #!, followed by an interpreter directive

#!/usr/bin/env python3

print("Hello World!")

Step 2: Add execution permission

Run the following chmod command. It will grant the owner of the file execution permissions:

chmod u+x hello.py

You can now run the script without prefixing it with python3. To execute the script, use the following command:

./hello.py

Output:

Hello World!

You have learned how to make your scripts standalone and grant them execution permissions. Change your scripts so you can open them from a shell without having to use the python command in front of it.

Dive deeper into this topic with training?
By using this site, you acknowledge that you have read and understand our Cookie and Privacy Policy.