Skip to main content

Command Palette

Search for a command to run...

Streamlining the execution of Python scripts

Updated
1 min read
Streamlining the execution of Python scripts

In recent months, I've been dabbling in a bit of Python. It's easy to get to grips with, and the syntax is clean. Closely following a variety of tutorials, I'd simply write some code in hello-world.py, then execute it like so:

chris@chris-book:~$ python3 hello-world.py

This would work, and I thought that's just how you ran the code. Anyway, while starting a new course, I just discovered that you can streamline this process using a shebang:

  1. Make the file executable using chmod (make sure you consider which users/groups should be permitted to do this, obviously)

  2. Append #!/bin/python3 to the start of the file

     #!/bin/python3
     print("Hello World");
    
  3. Execute the file

     chris@chris-book:~$ ./hello-world.py
     Hello World
    

Keep in mind that you can do this with other program files too.