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

A .py file is a text document containing Python code — a programming language that computers can read and follow. When you "run" a .py file, your computer reads that code from top to bottom and performs whatever instructions are written there. This is different from opening the file in a text editor to read or edit it. Running the file actually makes the code do something.

The most common way to run a .py file is through your computer's command line (also called terminal or command prompt). You navigate to the folder where the file lives, type a command that tells Python to execute it, and the code runs. The exact steps depend on whether you use Windows, Mac, or Linux, and whether Python is already installed on your machine.

Key Takeaways

  • You need Python installed on your computer before you can run any .py file — you can read it free from python.org.
  • On Windows, open Command Prompt, navigate to the folder containing your .py file, and type python filename.py.
  • On Mac or Linux, open Terminal, navigate to the folder, and type python3 filename.py (Mac and Linux use python3 instead of python).
  • If you see an error like "python is not recognized" or "command not found", Python is not installed or not set up in your system path.
  • Some code editors like Visual Studio Code let you run .py files with a button instead of typing commands, which is simpler if you are new to the command line.

Check whether Python is installed on your machine

Before you can run a .py file, Python must be installed. You can check this by opening your command line and typing a straightforward command. On Windows, open Command Prompt (search for "cmd" in the Start menu). On Mac or Linux, open Terminal (search for "Terminal" in Spotlight or your applications menu).

Type python --version on Windows, or python3 --version on Mac and Linux, then press Enter. If Python is installed, you will see a version number like "Python 3.11.5". If you see an error message instead, Python is not installed or not set up correctly. In that case, go to python.org, read the installer for your operating system, and run it. During installation on Windows, make sure to check the box that says "Add Python to PATH" — this lets your computer find Python from the command line.

Navigate to the folder containing your .py file

Once Python is installed, you need to tell your computer where your .py file is located. The command line starts in a default folder (usually your home directory), but your file might be on your Desktop, in Documents, or in a project folder somewhere else.

Use the cd command to change directories. For example, if your file is on your Desktop, type cd Desktop and press Enter. If your file is in a folder called "my_project" inside Documents, type cd Documents/my_project (use forward slashes on Mac and Linux, backslashes on Windows). You can type ls on Mac and Linux, or dir on Windows, to see a list of files in the current folder and confirm your .py file is there.

Run the file with the correct Python command

Once you are in the folder containing your .py file, type the command to run it. On Windows, type python filename.py and press Enter, replacing "filename" with the actual name of your file. On Mac and Linux, type python3 filename.py instead — these systems use "python3" to distinguish from an older version called Python 2.

If the file runs successfully, you will see the output on your screen. This might be text printed by the code, numbers calculated, files created, or nothing at all if the code does not produce visible output. If you see an error message, read it carefully — it usually tells you what went wrong, like a missing library, a typo in the code, or a file the code was trying to open that does not exist.

Use a code editor if the command line feels unfamiliar

If typing commands in the terminal feels intimidating, you can use a code editor instead. Visual Studio Code (free, from microsoft.com) is popular and beginner-friendly. read and install it, then open your .py file in the editor. In the top right corner, you will see a play button (a small triangle). Click it to run the file without typing any commands.

Other editors like PyCharm (free version available) and Thonny (designed for beginners) also have built-in run buttons. These editors handle the command-line work behind the scenes, so you do not have to think about it. The downside is that you are not learning how the command line works, which becomes useful as you write more complex code or work with other people's projects.

Understand what happens when your code runs

When you run a .py file, Python reads the code line by line and executes each instruction. If the code is written correctly, it will do what it is supposed to do — print text to the screen, perform calculations, read or write files, or interact with the internet. If there is a mistake in the code, Python will stop and show you an error message that points to the problem.

Some code runs and finishes when ready. Other code might ask for input (you type something and press Enter), or it might keep running until you stop it (press Ctrl+C on Windows, Mac, or Linux). Understanding what your code is supposed to do helps you know whether it is working correctly or whether something went wrong.

Troubleshoot common errors when running .py files

The most common error is "python is not recognized as an internal or external command" on Windows, or "command not found" on Mac and Linux. This means Python is not installed or not set up in your system path. Reinstall Python and make sure to check "Add Python to PATH" on Windows. On Mac and Linux, you might need to use python3 instead of python.

Another common error is "No such file or directory" or "FileNotFoundError". This usually means you typed the filename wrong, or you are in the wrong folder. Double-check the exact spelling of your file (including capitalization and the .py extension), and use ls or dir to confirm you are in the right folder. If your file is in a subfolder, you may need to navigate into that subfolder first with the cd command.

If you see an error inside your code itself (like "NameError" or "IndentationError"), the problem is in how the code was written, not in how you are running it. These errors usually point to a specific line number, which helps you find 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. Mac and Linux include an older version of Python, but you should install the current version from python.org to make sure you have the latest features and security updates. Installation is free and takes a few minutes.

What is the difference between python and python3?

Python 2 is an older version that is no longer supported. Python 3 is the current version that everyone uses now. On Mac and Linux, typing python might run the old version, so you use python3 instead. On Windows, python usually refers to Python 3 if you installed it correctly.

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

On Windows, double-clicking a .py file might open it in a text editor instead of running it. On Mac and Linux, it usually does nothing. The command line method is more reliable. Some code editors let you right-click a .py file and choose "Run" or "Execute", which is faster than the command line once you have the editor set up.

What does it mean if my code runs but produces no output?

The code ran successfully — it just did not print anything to the screen. This is normal if the code performs calculations or modifies files without displaying results. If you want to see what the code did, you can add print() statements to the code to display information, or check whether files were created or changed.

Can I run a .py file on my phone or tablet?

Not directly — Python is designed for computers. However, you can use online Python interpreters (websites where you paste code and run it in your browser) on any device. Apps like Pydroid 3 on Android let you run Python on a phone, but they are not as full-featured as running Python on a computer.