Option 1: Call the interpreter

  • For Python 2: python <filename>.py
  • For Python 3: python3 <filename>.py

Option 2: Let the script call the interpreter

  1. Make sure the first line of your file has #!/usr/bin/env python.
  2. Make it executable - chmod +x <filename>.py.
  3. And run it as ./<filename>.py
Answer from abhshkdz on askubuntu.com
🌐
Real Python
realpython.com › run-python-scripts
How to Run Your Python Scripts and Code – Real Python
2 weeks ago - Unix systems require executable permissions and a shebang line like #!/usr/bin/env python3 to run scripts directly as programs. The python command’s -m option runs Python modules by searching sys.path rather than requiring file paths. IDEs like PyCharm and code editors like Visual Studio Code provide built-in options to run scripts from the environment interface. To get the most out of this tutorial, you should know the basics of working with your operating system’s terminal and file manager.
🌐
Digis
digiscorp.com › how-to-run-python-file-in-terminal
How to Run Python File in Terminal?
September 4, 2026 - Fast execution and testing You can edit a .py file, execute it immediately, inspect the output, and repeat the process without creating a full IDE run configuration. Direct control over the Python interpreter Terminal commands make it easier to check which Python version is running, select a specific interpreter, and diagnose environment-related problems.
Discussions

How to run Python Terminal
I am using Windows operating machine. In start menu I found “Python 3.12.7 (64 bit)” But when I type python-v I am not able to get the python version. I think I am missing packages maybe. Kindly have a look at the attached image and let me know how to fix the problem. More on discuss.python.org
🌐 discuss.python.org
5
0
December 17, 2024
what is the simplest way to run a a python script?
I would be very careful before letting code, produced by Chatgpt, play with my file system. More on reddit.com
🌐 r/learnpython
22
8
February 13, 2023
Running Python Programs from a Terminal
Well what are you trying? What command exactly? Can you show us the complete error you are getting? Also, what OS are you using and how did you install python? More on reddit.com
🌐 r/learnpython
16
2
August 2, 2022
How to I execute a .py file from the interpreter?
I'm assuming that the course is talking about running python3 -i report.py, which will run report.py normally and then drop you into a python interpreter after the program is done running. This python interpreter will have all of your python file's functions, variables, etc so that you can check the value of things. It's an interesting feature for debugging, although I don't use it personally More on reddit.com
🌐 r/learnpython
9
2
June 6, 2021
🌐
Visual Studio Code
code.visualstudio.com › docs › python › run
Running Python code in Visual Studio Code
November 3, 2021 - Select one or more lines, then press Shift+Enter, or right-click and select Run Selection/Line in Python Terminal. This option is convenient for testing just a part of a file. Place your cursor on a line of code and press Shift+Enter to activate ...
🌐
vteams
vteams.com › blog › how-to-run-a-python-script-in-terminal
How to Run a Python Script in Terminal Step by Step Guide
April 7, 2025 - Now, when you run your Python script in the terminal, you can provide the specified arguments, and your script will process them accordingly. Executing a Python script within a terminal is an essential skill for every Python programmer. Whether you’re using Linux, Windows, or an integrated development environment like VSCode, the process is straightforward.
🌐
Oxylabs
oxylabs.io › blog › how-to-run-a-python-script-in-terminal
How To Run A Python Script In Terminal
This guide demonstrates how to execute Python scripts directly from the command line on Windows, macOS, and Linux by using `python` or `python3` followed by the script's path. It covers navigating directories to run scripts and also provides instructions for executing Python files within VS Code. A computer's terminal is essential for developers, especially in GUI-less server environments.
🌐
GeeksforGeeks
geeksforgeeks.org › python › open-and-run-python-files-in-the-terminal
Open and Run Python Files in the Terminal - GeeksforGeeks
July 23, 2025 - The Linux terminal offers a powerful environment for working with Python files, providing developers with efficient ways to open, edit, and run Python scripts directly from the command line. In this article, we'll explore various techniques and commands for handling Python files in the Linux terminal, empowering developers to streamline their workflow and enhance productivity. ... 2. Use the cd command to change the present working directory to the directory in which the Python program file is saved or in which you want to create the program file.
Find elsewhere
🌐
Python.org
discuss.python.org › python help › how to run python terminal
How to run Python Terminal - Python Help - Discussions on Python.org
December 17, 2024 - I am using Windows operating machine. In start menu I found “Python 3.12.7 (64 bit)” But when I type python-v I am not able to get the python version. I think I am missing packages maybe. Kindly have a look at the atta…
🌐
Reddit
reddit.com › r/learnpython › what is the simplest way to run a a python script?
r/learnpython on Reddit: what is the simplest way to run a a python script?
February 13, 2023 -

i have this script that chatgpt made for me, i want to test it out but i am new to coding and im not sure how to run it.
i tried googling it but many of them go into way to deep detail and have just walls of text that for someone with dyslexia is very hard to read. i have python and pip installed and they are up to date along with notepad++

import os

def rename_files_in_folder(folder_path, prefix, exclude_filename=None):
    # Get a list of all files in the folder
    files = os.listdir(folder_path)

    # Rename each file with a prefix and number, excluding the specified file
    for i, filename in enumerate(files):
        if filename == exclude_filename:
            continue
        new_filename = f"{prefix}{i + 1}{os.path.splitext(filename)[1]}"
        os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_filename))

# Example usage
rename_files_in_folder("/path/to/folder", "new_file_", exclude_filename="file_to_exclude.txt")
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-run-a-python-script
How to Run a Python Script - GeeksforGeeks
Python Interactive Mode allows you to execute Python statements directly in the interpreter. Open Command Prompt or Terminal, type python, and press Enter. ... The print() statement is executed immediately.
Published: September 22, 2026
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to run a python script
How to Run a Python Script: Beginner's Complete Guide
March 28, 2026 - How to run a Python script from the command line is the standard way to execute a .py file. You open a terminal, point Python to the script, and the interpreter runs it right away.
🌐
Altcademy
altcademy.com › blog › how-to-run-a-python-script-in-terminal
How to run a Python script in terminal - Altcademy.com
September 11, 2023 - If you have used Windows, you might be familiar with the command prompt or Powershell. On macOS, it's called Terminal. On Linux, it could be called Terminal, Konsole, or a bunch of other names. A Python script is a set of commands written in Python programming language that you can run as a single program.
🌐
Dataconomy
dataconomy.com › 2026 › 10 › 28 › how-to-run-python-script-terminal-code-editor
How To Run Python Scripts Using The Terminal Or A Code Editor - Dataconomy
1 week ago - To run a Python script, open a terminal and ask the Python interpreter to execute your saved file. This guide
🌐
LabEx
labex.io › tutorials › python-how-to-run-a-python-program-from-the-command-line-398242
How to run a Python program from the command line | LabEx
The most common way to run a Python program is by writing your code in a file with a .py extension and then executing that file using the Python interpreter from your terminal.
🌐
Reddit
reddit.com › r/learnpython › running python programs from a terminal
r/learnpython on Reddit: Running Python Programs from a Terminal
August 2, 2022 -

I've new to python and trying to learn the computer language to experiment and hopefully be good enough to get a job. I bought "Python Crash Course" 2nd edition by Eric Matthes and have been researching youtube videos on the language to learn more about the program. I don't know if its just my computer or I didn't set it up right but its impossible for me to open or run a python program from my terminal. I always get hit with the "The system cannot find the path specified" whenever I try and it's becoming really frustrating. I watched a couple videos online but nothing seems to work. How do i fix this little issue?

🌐
DataCamp
datacamp.com › home › tutorials › python › how to run python scripts tutorial
How to Run Python Script, Running Python Code Tutorial | DataCamp
December 21, 2022 - Then, open the terminal and go to the directory where the code resides and run the script with a keyword python followed by the script name. To create the terminal.py file, use vim in the terminal with the program name as vim terminal.py and paste the below code in it.
Top answer
1 of 4
28

There's two things needed.

  1. A script must have #! line telling the OS which interpreter to use. In your case your very first line in the code must be #!/usr/bin/env python3
  2. You need to open file manager , go to Edit -> Preferences -> Behavior, and select what to do with executable files

    1. Finally , make sure your file itself actually has executable permissions set. In terminal you can do chmod +x /path/to/script.py and in GUI, right click on the file and alter its Properties -> Permissions

Note about shebang line

The very first line is called shebang line and must start with #! ; whatever comes next is the name of the interpreter that will read your code. In case you are using python3 you could use either #!/usr/bin/python3 or #!/usr/bin/env python3 for portability. If you are not using code that will be specific to python version - just use #!/usr/bin/env python

Note on the script output:

If your script prints output to console, it will need to have terminal window, or alternatively use GUI dialogs such as zenity. Prefer using Run in Terminal option if you want to see the code. If you want the script to do something without seeing console output - use Run option.

In addition, if you have command line parameters , such as sys.argv[1] in the script , you can't set them unless you have terminal window open.

2 of 4
3

You need to put the location of the program to execute your code on the first line and you then need to set the script to run as an executable by changing its permissions. This assumes you're launching your applications from terminal or another script.

Find your Python installation

$ which python
/usr/bin/python

Add the programs location to the top line of your program with a #! in front

#!/usr/bin/python

# Python code goes here....

Set the Python script to have execution rights

$ chmod 700 test.py

Now you can run the script directly

$ ./test.py

Code listing for test.py

#!/usr/bin/python

print "test"
🌐
FavTutor
favtutor.com › blogs › run-python-file-terminal
How to Run a Python File in Terminal? (Step-by-Step)
November 14, 2023 - In this article, we will explore various techniques and best practices for running Python scripts in the terminal. A Python script is a file written in the Python programming language and saved with a ".py" extension. It contains a sequence of instructions or code that can be executed by a Python interpreter.
🌐
Decodo
decodo.com › blog › how-to-run-python-code-in-terminal
How to Run Python Code in Terminal
4 weeks ago - Verify Python installation by checking installed programs ... Always check the PATH option during Python installation and test commands immediately after installation. If you get "Permission denied" when running scripts or installing packages, try the following: Use chmod +x script.py to make scripts executable (macOS/Linux) Run the terminal as an administrator when necessary (Windows)
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › how-to-run-python-scripts
How to Run Python Scripts: A Step-by-Step Guide
February 6, 2024 - This process consists of several steps that enable users to run Python code effortlessly. ... On Windows, press Win + R, type "cmd," and press Enter. On macOS or Linux, open the Terminal.
Address: Reflex, Bracknell