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.
Create a Python script and save as ‘hello.py’.
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:
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!")
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:
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.