October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Create and Run a Python File in Terminal

Create a Python script in a text editor, save it with a .py extension, and run it from your system terminal with the Python command available on your computer.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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 python or python3, 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

  1. 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.
  2. Create or open a file in your editor. Give it a clear name ending in .py, such as hello.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.
  3. Enter Python code. For a first test, type print("Hello, world!").
  4. Save the file. Make sure the editor has saved the latest contents before running it.
  5. Run it from the system terminal prompt. In the same folder as the file, try python hello.py. If your installation uses python3, run python3 hello.py instead. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.