How to Execute Python Scripts?

Python is a versatile scripting language that can be used for a wide variety of tasks, from automating simple tasks to building complex applications.

While Python scripts can be written and executed directly from the interpreter, they can also be saved as standalone files and executed from the command line.

In this article, we’ll show you how to execute a Python script from the command line.

How to Write a Python Script

Python scripts are written in plain text, which means they can be written in any text editor, such as Microsoft Notepad or Apple TextEdit. However, we recommend using a professional code editor like Atoms or Visual studio code which has features like syntax highlighting and autocompletion that make writing code easier.

Once you have a text editor open, you can start writing your script. For our example, we’ll keep it simple and just print out the phrase “Hello, world!” To do this, we’ll use the print() function, which takes a string (or any other data type) as an input and prints it to the screen. Here’s what our script looks like:

print("Hello, world!")
Code language: PHP (php)

Save your file with a “.py” extension; for example, “”. You can name your file anything you want, but be sure to use the “.py” extension so that your computer knows it’s a Python script.

How to Execute a Python Script

To run your Python script, open the terminal and navigate to the folder where your script is saved. Then type “python”, replacing “SCRIPTNAME” with the name of your Python file. This will execute your script and print the output to the terminal.

$ python Hello, world!

How to Execute Python Scripts on macOS?

On macOS, things are a bit simpler because the operating system comes with a built-in interpreter called Python Launcher, which allows you to run scripts without having to install anything else first.

To launch a script using Python Launcher, simply open Finder and navigate to the location of your script (e.g., Documents/myscripts/ Right-click on the script and select Open With >Other >Python Launcher from the context menu.

This will open a new window with several options; make sure that “Run Script Directly” is checked and then click OK.

How to Execute Python Scripts on Windows?

On Windows, you can use the command prompt or the PowerShell to execute a Python script. If you want to use the command prompt, you will need to add the path of your Python interpreter by using the following command:

set path=%path%;"C:\Users\ACER\AppData\Local\Microsoft\WindowsApps\python.exe"
Code language: JavaScript (javascript)

Replace”C:\Users\ACER\AppData\Local\Microsoft\WindowsApps\python.exe” with the actual path of your Python interpreter.

After that, you can go ahead and execute your script by typing python

If you want to use PowerShell, you can launch it by searching for PowerShell in the Start Menu.

Then, you need to type cd C:\Users\YourName\Documents\mypythonfiles (replace YourName with your actual username), which will change your current directory to the one where is located.

Finally, type python and press Enter to run your script.

Remember that if your Python script has spaces in its name, you need to surround the name with quotation marks like this: “”. Also bear in mind that both commands are case-sensitive, so make sure that you type them exactly as shown above.

How to Execute Python Scripts on Linux?

If you’re using Linux, there’s a good chance that your distribution already has a version of Python installed by default;

However, it might not be up-to-date so it’s always best practice to check before proceeding any further—to do that simply enter python -V into a terminal window;

$python -V
Code language: PHP (php)

if it returns something like “Python 3.10” then proceed with the tutorial below; if not write sudo apt install python3 into terminal then hit Enter (this command will install Ubuntu’s repositories’ latest stable release of Python 3)

$sudo apt install python3
Code language: PHP (php)

. To launch your scripts from the Terminal app simply change directories until you reach the one housing said file e.g., cd ~/Documents/myscriptfiles) and then type python3 for example — remember if your file name contains spaces place quotation marks around said name (“”). Once done hit Enter/Return key!

Code language: PHP (php)

Writing and executing a Python script is a simple process that doesn’t require any special tools or knowledge. All you need is a text editor and a terminal. With just these two things, you can write and execute many different kinds of Python scripts.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts