The fastest way depends on what you have installed

To open a Python file, you need Python installed on your computer and a way to run the code inside it. The simplest method is to double-click the file if Python is set up to open .py files by default — but this only works on Windows and only if you chose that option during installation. On Mac and Linux, double-clicking usually opens the file in a text editor instead. The most reliable method across all systems is to open a terminal or command prompt and type a command that tells Python to run the file.

What happens when you open a Python file depends on what the code does. Some files print results to the screen and close immediately. Others open a window, wait for input, or run in the background. If the window closes too fast to read the output, you need to run it from a terminal instead of double-clicking, so you can see what happened.

Key Takeaways

  • On Windows, you can double-click a .py file to run it if Python was installed with that option enabled, but a terminal command works on all systems.
  • The terminal command is python filename.py on Windows or python3 filename.py on Mac and Linux.
  • If you get a "command not found" error, Python is not installed or not in your system's PATH, and you need to install it or find where it lives on your computer.
  • Text editors and IDEs like Visual Studio Code let you open Python files and run them with a button click, which is useful if you plan to edit the code.
  • If a window closes immediately after running, open the file in a terminal instead so you can read the output before it disappears.

Opening a Python file by double-clicking on Windows

On Windows, if you installed Python and checked the box that says "Associate files with Python", you can double-click any .py file and it will run. Right-click the file, select "Properties", and look at the "Opens with" field — if it says Python, you are set. If it says Notepad or another text editor, Python is not associated with the file type, and you need to use a terminal instead.

When you double-click a Python file, a command window opens, the code runs, and the window closes when finished. If the program finishes in less than a second, you may not see any output at all — the window closes too fast. To see what the code printed, run it from a terminal instead, where the window stays open until you close it manually.

Running Python from a terminal or command prompt

Open a terminal (Mac or Linux) or command prompt (Windows). On Windows, press the Windows key, type "cmd", and press Enter. On Mac, press Command + Space, type "terminal", and press Enter. On Linux, right-click the desktop or use your application menu to open a terminal.

Navigate to the folder where your Python file lives. Type cd followed by the path to the folder. For example, cd Desktop moves you to the Desktop folder. Then type the command to run Python. On Windows, type python filename.py. On Mac and Linux, type python3 filename.py, replacing "filename" with the actual name of your file.

The code runs and prints any output to the terminal. The terminal stays open, so you can read the results. If you get an error that says "python: command not found" or "'python' is not recognized", Python is not installed on your computer or not in your system's PATH — the list of places your computer looks for programs. You can install Python from python.org, or ask your system administrator for help.

Using a text editor or IDE to open and run Python files

Text editors and integrated development environments (IDEs) let you open a Python file, see the code highlighted with color, and run it with a button click. Visual Studio Code is free and popular — download it, install the Python extension from the Extensions marketplace, then open your .py file and click the play button in the top right corner. PyCharm Community Edition is free and built specifically for Python — it opens .py files and has a green play button to run them.

These tools are useful if you plan to read or change the code, because they show you line numbers, catch some errors before you run the file, and let you step through the code line by line to see what is happening. If you only need to run a file once and do not care about editing it, a terminal is faster.

What to do if the file does not open or run

If you double-click a .py file and it opens in a text editor instead of running, Python is either not installed or not associated with .py files. Install Python from python.org. During installation on Windows, check the box that says "Add Python to PATH" — this lets your computer find Python from a terminal. Also check "Associate files with Python" if you want to double-click files to run them.

If you run a file from a terminal and get an error like "No such file or directory", the terminal is looking in the wrong folder. Type pwd (Mac and Linux) or cd (Windows) to see where you are, then navigate to the correct folder with cd. If you get an error inside the Python code itself — like "NameError" or "SyntaxError" — the file has a problem in the code, not in how you opened it. Read the error message to find the line number and the issue.

Choosing between methods based on what you are doing

Double-clicking works if you just want to run a file and do not need to see the output or edit the code. Use a terminal if the program finishes quickly and you need to read what it printed, or if you want to pass arguments to the program (extra information the code reads when it starts). Use a text editor or IDE if you plan to read the code, change it, or debug it — these tools show you the code and let you run it without switching windows.

If you are learning Python and writing your own files, an IDE like PyCharm or Visual Studio Code saves time because you can write and run code in the same window. If you are running someone else's file and just need to see the results, a terminal is the most direct path.

Frequently Asked Questions

Why does the window close immediately after I double-click a Python file?

The program finished running and closed the window automatically. To see the output, run the file from a terminal instead — type python3 filename.py and the terminal window stays open so you can read the results.

What is the difference between python and python3?

Python 2 is old and no longer supported. Python 3 is the current version. On Mac and Linux, python sometimes points to Python 2, so you use python3 to make sure you run Python 3. On Windows, python usually means Python 3. If one does not work, try the other.

Can I open a Python file in Notepad or Word?

Yes, but opening it does not run the code — it just shows you the text inside. Notepad works fine for reading code. Word will add formatting and may corrupt the file. To actually run the code, use a terminal, double-click it (if Python is associated), or open it in a text editor or IDE with a run button.

Do I need to install anything besides Python to run a .py file?

Python itself is all you need for basic files. Some files use libraries — extra code that does not come with Python — and you have to install those separately using a tool called pip. The error message will tell you which library is missing and how to install it.