How to Get a Python Source File to Run a Program on Your Desktop

You have Python installed and a text file full of code — now what? Getting that code to actually run on your desktop comes down to understanding three things: what a source file is, where the Python interpreter lives, and how to point one at the other from a terminal.

Every Python program begins as a plain text file with a .py extension. You write your code in a text editor, save the file, then hand it to Python to execute. The terminal is the bridge between the two. This tutorial walks through each step in plain language so you leave with a working program running on your own machine.

What a Python Source File Actually Is

A Python source file is nothing more than a plain text document that stores Python code. There is no special binary format, no compiler, no packaging step required for simple programs. When you type Python code and save it as hello.py, that file becomes your source.

The .py extension is not strictly required by the interpreter — you could technically name the file anything — but it is the universal convention and it tells your editor, operating system, and other developers what kind of file it is. Always use .py.

Note

A source file is different from an executable (.exe) file. Python files stay as readable text. The interpreter translates them into machine instructions each time you run them.

Source files can live anywhere on your computer — your desktop, a projects folder, a USB drive. The location matters only because you need to tell Python where to find the file when you run it, which you do from the terminal.

Built-in editor
Notepad — simple, always available, no extra install needed. Save the file with a .py extension and you are set.
Recommended editor
VS Code — free, has Python syntax highlighting, and includes an integrated terminal so you can run your file without switching windows.
Built-in editor
TextEdit — switch it to plain text mode first (Format > Make Plain Text), otherwise it saves in rich text format which breaks Python.
Recommended editor
VS Code or BBEdit — both handle Python files well and let you run code directly from within the application.
Built-in editor
gedit, nano, or any text editor included with your distribution. All write plain text by default.
Recommended editor
VS Code or Vim — VS Code for a graphical experience, Vim for a fully terminal-based workflow once you are comfortable with the command line.

What the Python Interpreter Does with Your File

When you run a .py file, you are handing it to the Python interpreter — a program called python or python3 that is already installed on your machine. The interpreter reads your source code, converts it to an intermediate form called bytecode, and then executes the bytecode. This all happens automatically and very quickly.

You do not have to do anything special to trigger this process. The single command python filename.py starts the interpreter and points it at your file. Everything else happens behind the scenes.

Pro Tip

Run python --version in your terminal first. It tells you exactly which version you have and confirms Python is on your system PATH. If you see "Python 3.x.x" you are ready to go.

The Python interpreter processes your file from top to bottom, one statement at a time. If it hits an error anywhere, it stops and prints a traceback — a message that tells you exactly which line went wrong and why. That traceback is your friend. Reading it carefully is the fastest way to fix a problem.

code builder click a token to place it

Build the terminal command that runs a Python file called greet.py:

your command will appear here...
greet.py pip python import run greet
Why: The command is python greet.py. You type the interpreter name first (python), then the filename including its extension (greet.py). pip is the package installer, not the interpreter. import is a Python keyword used inside code, not on the command line. run and greet without the extension are not valid commands here.

Writing Your First .py File

Open your text editor and type the following code exactly as shown. It is a small program that prints a greeting and then asks for your name.

python
# my first Python program
# save this file as hello.py

print("Hello! This program is running on your desktop.")

name = input("What is your name? ")
print("Good to meet you,", name)

Save the file as hello.py. Pay attention to a few things in this code. The lines beginning with # are comments — Python ignores them. The print() function sends text to the terminal. The input() function pauses the program and waits for you to type something, then stores whatever you typed in the variable name.

Watch Out

On Windows, Notepad sometimes adds a .txt extension automatically. In the Save As dialog, set "Save as type" to "All Files" and type hello.py manually to prevent this.

Indentation matters in Python, but this particular program has no indented blocks, so you do not need to worry about that yet. Every line starts at column zero. When you start writing functions and loops, indentation becomes critical — Python uses it to define code structure instead of curly braces.

spot the bug click the line that contains the bug

The program below should print a greeting and ask for a name. One line contains a bug. Click the line you think is wrong, then hit check.

1 print("Hello from Python!")
2 name = input("What is your name? ")
3 print("Nice to meet you, " + Name)
4 print("Hope you enjoy Python.")
The fix: Change Name to name on line 3. Python variable names are case-sensitive. The variable was assigned as name (lowercase) on line 2. Referencing it as Name (capital N) causes a NameError because Python treats them as two different identifiers.

How to Run a Python Source File as a Desktop Program

Follow these steps in order. Each one builds on the previous. The whole process takes about two minutes once you have done it once.

  1. Verify Python is installed

    Open your terminal (macOS: Spotlight search for "Terminal"; Windows: search for "Command Prompt" or "PowerShell"; Linux: press Ctrl+Alt+T). Type python --version or python3 --version and press Enter. You should see something like Python 3.12.2. If you see an error instead, visit python.org/downloads and install Python before continuing. On Windows, check the "Add Python to PATH" box during installation.

  2. Create and save your .py source file

    Open your text editor and write your Python code. Save the file with a .py extension in a location you can easily reach from the terminal — your Desktop or a folder called projects works well. For this tutorial, save the earlier example as hello.py on your Desktop.

  3. Navigate to the file's folder in the terminal

    Use the cd command (change directory) to move to the folder where your file is saved. On macOS and Linux type cd ~/Desktop. On Windows type cd %USERPROFILE%\Desktop. After pressing Enter, type ls (macOS/Linux) or dir (Windows) to list the files in that folder and confirm hello.py appears.

  4. Run the file with the Python interpreter

    Type python hello.py and press Enter. On macOS and Linux you may need python3 hello.py instead. Python reads the file, executes each line in order, and your program starts. The greeting prints immediately and the terminal waits for your name input.

  5. Read the output or traceback

    If the program ran successfully, you see your output in the terminal. If something went wrong, Python prints a traceback ending with the error type and message — for example, NameError: name 'Name' is not defined. The line number in the traceback tells you exactly where to look. Fix the error in your text editor, save the file, and run the command again.

terminal
# macOS / Linux
$ cd ~/Desktop
$ python3 hello.py
Hello! This program is running on your desktop.
What is your name? Kandi
Good to meet you, Kandi

# Windows (Command Prompt)
C:\> cd %USERPROFILE%\Desktop
C:\Users\YourName\Desktop> python hello.py
Hello! This program is running on your desktop.
What is your name? Kandi
Good to meet you, Kandi
"Code is like humor. When you have to explain it, it's bad." — Cory House, software developer and educator

Python Learning Summary Points

  1. A Python source file is a plain text file saved with a .py extension. No compiler or special build step is needed for simple programs.
  2. The Python interpreter is the program that reads your .py file and executes it. You invoke it from the terminal with python filename.py or python3 filename.py.
  3. The cd command moves you between folders in the terminal. You must be in the same folder as your file — or provide the full path — for the interpreter to find it.
  4. Python variable names are case-sensitive. name and Name are two different identifiers. Mismatched casing is one of the most common causes of NameError in beginner code.
  5. When a program fails, Python prints a traceback. Read it from the bottom up: the last line states the error type and message, and the line above it shows the exact line number where the problem occurred.

Running a Python file from the terminal is a skill you will use every day. The workflow — edit the file, save, run the command, read the output — becomes second nature quickly. Once you are comfortable here, the next step is learning to pass data into your program using command-line arguments and to organize larger programs across multiple files.

check your understanding question 1 of 5

Frequently Asked Questions

A Python source file is a plain text file that contains Python code and is saved with the .py extension. When you pass this file to the Python interpreter, it reads and executes the instructions line by line.

Open your terminal or command prompt, navigate to the folder containing your .py file using the cd command, then type python filename.py (or python3 filename.py on macOS and Linux) and press Enter.

On many macOS and Linux systems, python refers to Python 2 and python3 explicitly targets Python 3. On Windows with a modern Python installation, python usually maps to Python 3. Always verify with python --version or python3 --version.

This error means Python is not installed, or the installer did not add Python to your system PATH. On Windows, re-run the installer and check the "Add Python to PATH" checkbox. On macOS, install Python from python.org or use Homebrew.

Any plain text editor works — Notepad on Windows, TextEdit (plain text mode) on macOS, or gedit on Linux. Many beginners quickly move to VS Code, which adds syntax highlighting and an integrated terminal.

The print() function sends output to the terminal. Whatever you place inside the parentheses — a string, a number, or a variable — appears on the screen when the program runs.

Yes. The .py extension tells both your operating system and your text editor that the file contains Python code. Without it, syntax highlighting will not work and some tools may not recognize the file as Python.

On macOS and Linux, type pwd (print working directory) and press Enter. On Windows Command Prompt, type cd with no arguments. Both commands show you the full path of your current folder.

For quick testing you can use online Python interpreters like the shell at python.org or repl.it. However, running programs on your own desktop requires a local Python installation.

Python will stop execution at the first error it encounters and print a traceback to the terminal. The traceback shows the file name, the line number, and a short description of what went wrong, which tells you exactly where to look.