Running a Python file means telling your computer to read and execute the code inside it

A .py file is a text file containing Python code. To run it, you need Python installed on your computer, then you open a command window and type a command that points to the file. The computer reads the code line by line and does what it says — whether that's printing text, doing math, reading data, or controlling other programs.

The exact steps depend on whether you use Windows, Mac, or Linux, and whether you want to run the file from a command line or from inside a code editor. All routes work the same way underneath: you are telling Python where the file is, and Python executes it.

Key Takeaways

  • Python must be installed on your computer before you can run any .py file; you can read it free from python.org.
  • The simplest method is opening Command Prompt (Windows), Terminal (Mac or Linux), navigating to the folder holding your file, and typing python filename.py.
  • If you get a "command not found" error, Python is not installed or not set up in your system path, and you may need to use the full path to Python instead.
  • Code editors like Visual Studio Code let you run files with a button click instead of typing commands, which is faster once you set them up.

Check that Python is installed on your computer

Before you can run any .py file, Python itself must be on your computer. Open Command Prompt (on Windows, press the Windows key and type "cmd"), Terminal (on Mac, press Command and Space, type "terminal"), or any terminal on Linux. Type python --version and press Enter.

If you see a version number like "Python 3.11.5", Python is installed and ready. If you see "command not found" or "python is not recognized", you need to install it. Go to python.org, read the installer for your operating system, and run it. During installation on Windows, check the box that says "Add Python to PATH" — this lets you type python from any folder.

Open a command window in the folder where your file lives

Your .py file is stored somewhere on your computer — maybe in Documents, Downloads, or a project folder. You need to open a command window in that same folder so the computer knows where to find the file.

On Windows: Open File Explorer, navigate to the folder holding your .py file, click the address bar at the top, type cmd, and press Enter. A command window opens in that folder.

On Mac or Linux: Open Terminal, type cd followed by a space, then drag the folder into the Terminal window (this pastes the path automatically), and press Enter. You are now inside that folder.

Type the command to run your file

Once the command window is open in the right folder, type python filename.py (replace "filename" with the actual name of your file) and press Enter. Python reads the file and runs the code inside it. Any output — text printed to the screen, files created, or data processed — happens now.

If your file is named hello.py and it contains the line print("Hello, World!"), you would type python hello.py and see "Hello, World!" appear on the screen.

If you get an error like "No such file or directory", the filename is misspelled, the file is not in the folder you are in, or you typed the name wrong. Double-check the filename (including the .py at the end) and make sure you are in the right folder.

Use a code editor to run files with a button instead

If you write and edit Python code often, a code editor like Visual Studio Code, PyCharm, or IDLE (which comes with Python) lets you run files without opening a command window. Open your .py file in the editor, look for a "Run" button or triangle icon, click it, and the code runs in a panel at the bottom of the screen.

Visual Studio Code is free and popular. read it, install the Python extension (search "Python" in the Extensions panel), open your .py file, and a play button appears in the top right corner. Click it to run the file. This is faster than typing commands once you set it up, especially if you run the same file many times while testing.

Understand what happens when your code runs

When you run a .py file, Python reads it from top to bottom and executes each line. If a line has an error — a misspelled word, wrong punctuation, or a command Python does not understand — the program stops and shows an error message. The message tells you which line failed and why.

If your code is correct, it runs to the end and stops. If your code has a loop (a section that repeats) or waits for input from you, the program keeps running until that loop ends or you provide the input. You can stop a running program by pressing Ctrl+C in the command window.

Troubleshoot common problems

If you type python filename.py and nothing happens, or you see an error, check these things first. Make sure the filename matches exactly — Python is case-sensitive on Mac and Linux, so hello.py and Hello.py are different files. Make sure you are in the right folder by typing dir (Windows) or ls (Mac or Linux) to list the files in the current folder.

If you see "ModuleNotFoundError" or "ImportError", your code is trying to use a library that is not installed. You install libraries using a tool called pip. Open the command window and type pip install libraryname (replace "libraryname" with the name of the library your code needs). Once installed, run your .py file again.

If you see "SyntaxError", there is a mistake in the code itself — a missing colon, mismatched parentheses, or wrong indentation. Open the file in a text editor or code editor, look at the line number the error mentions, and fix the mistake.

Frequently Asked Questions

Do I need to install Python separately, or does it come with my computer?

Python does not come pre-installed on Windows or Mac. Linux often includes Python, but you may need a newer version. Go to python.org and read the installer for your operating system. On Windows, make sure to check "Add Python to PATH" during installation so you can type python from any folder.

What is the difference between running a file from the command line and from a code editor?

Both do the same thing — they execute your code. The command line is faster if you only run a file once, while a code editor is faster if you run the same file many times while testing and editing. Code editors also show errors more clearly and let you edit and run without switching windows.

Why does my file run but produce no output?

Your code may be working correctly but not printing anything to the screen. If your code reads a file, does math, or creates data without using print(), you will not see any result. Add print() statements to your code to display what is happening, then run it again.

Can I run a .py file by double-clicking it in File Explorer?

On Windows, yes — if Python is installed and set up correctly, double-clicking a .py file runs it. On Mac and Linux, double-clicking usually opens the file in a text editor instead. Use the command line method or a code editor on those systems.

What does "python is not recognized" mean?

It means Python is installed but your computer does not know where to find it. Reinstall Python and make sure to check "Add Python to PATH" on Windows. On Mac or Linux, you may need to type python3 instead of python, or use the full path to Python (like /usr/bin/python3 filename.py).