Create a file with a .py extension, add and save Python code, then run the file from your system terminal with the Python interpreter installed on your computer. For example, the command may be python hello.py or python3 hello.py; the right name depends on your operating system and setup.
What you need before you start
- A terminal or command prompt, such as Terminal on macOS, a Linux terminal, or Windows Terminal.
- A text editor in which to create and save the file. You can use an editor you already have; the specific file-creation steps depend on the editor and shell.
- A Python interpreter available on your computer. The executable may be called
pythonorpython3, among other possibilities.
A Python script is ordinary source code saved in a file. When you pass that filename to Python, the interpreter reads and executes it. The Python command-line documentation illustrates this with python myscript.py.
Create and run a Python file
- Choose a folder. Open your system terminal and navigate to the directory where you want the script to live. The command to change directories varies by shell.
- Create or open a file in your editor. Give it a clear name ending in
.py, such ashello.py. The method for creating a file from the terminal differs between shells, so use your editor or a method specific to your environment rather than assuming one shell command works everywhere. - Enter Python code. For a first test, type
print("Hello, world!"). - Save the file. Make sure the editor has saved the latest contents before running it.
- Run it from the system terminal prompt. In the same folder as the file, try
python hello.py. If your installation usespython3, runpython3 hello.pyinstead. Python’s interpreter tutorial explains that giving the interpreter a filename makes it read and execute that script.
If the command succeeds, the terminal displays Hello, world!. If your file is in a different folder, navigate to that folder first or provide the file’s path to the interpreter.
Choose a workflow that fits your setup
| Workflow | How it works | Best fit |
|---|---|---|
| Terminal plus text editor | Create or open the .py file in an editor, save it, then run the filename from the terminal. |
You already have an editor or want to keep the workflow simple. File-creation steps depend on your editor and shell. |
| Visual Studio Code with its integrated terminal | Save the Python file in VS Code and run it through the editor’s terminal, following its Python setup guidance. | You want to edit and run code in one application and have completed the setup described in VS Code’s documentation. |
VS Code’s guide to running Python code shows python3 hello.py on macOS and Linux and python hello.py on Windows. These are examples for the environments described in that guide, not a guarantee that every computer uses the same command.
#1 Best Overall
Make sure you are typing at the right prompt
Run python hello.py or python3 hello.py at your system terminal prompt. Do not type the whole command at Python’s interactive >>> prompt: interactive mode is a separate way to enter Python statements, while a filename argument runs a saved script.
Troubleshoot a command that does not work
“Command not found” or “not recognized”
The interpreter may not be installed, or it may be exposed under a different command name. Try the command appropriate to your platform and installation—such as python3 instead of python—and consult your Python installation’s guidance if neither is available.
Rank #2
Python cannot find the file
Check that the terminal is in the directory containing the file, that the filename and extension match exactly, and that you saved it. Alternatively, pass the file’s path to the interpreter.
The script runs but shows no expected output
Confirm that the saved file contains the code you intended, including a statement such as print("Hello, world!"), then save and run it again. The interpreter executes the contents of the file it receives, so unsaved editor changes are not part of that run.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




