Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
script
execute
run
linux

How to run a Python script in Linux

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

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.

RELATED TAGS

python
script
execute
run
linux

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring