a shot of dev knowledge


How to run a Python script in Linux

Python programs are very similar to text files; they can be written with something as simple as a basic text editor. The choice of available editors varies by the operating system in use. Let’s have a look at how we can run a Python script in Linux.

svg viewer

Running a Script

Once the script has been written, save it to a specific location in your system and then follow the steps below to run it:

  • Open the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T.

  • Navigate the terminal to the directory where the script is located using the cd command.

  • Type python SCRIPTNAME.py in the terminal to execute the script.

If the script is python3, use python3 in the terminal command:python3 SCRIPTNAME.py

Alternate method

If you find it cumbersome to write python in the terminal every time you run the script, follow the procedure below:

  • Prepend #! /usr/bin/python with your script.

  • Run the following command in your terminal to make the script executable: chmod +x SCRIPTNAME.py

  • Now, ​simply type ./SCRIPTNAME.py to run the executable script.



View all Courses

Keep Exploring