The simplest way to open a Python file depends on what you want to do with it

If you want to read the code inside a Python file, you can open it like any text document — right-click the file, choose "Open with", and pick Notepad (Windows), TextEdit (Mac), or any text editor. The file will show you the lines of code written by the programmer.

If you want to run the program that the Python file contains, you need Python installed on your computer first, then you execute the file through your operating system's command line or by double-clicking it (if it is set up that way). This actually makes the program do what it was written to do — process data, display output, or perform calculations.

Most people who work with Python files do one of these two things: they either read and edit the code in a text editor, or they run the file to see what it does. This guide covers both paths so you can choose the one that matches what you are trying to accomplish.

Key Takeaways

  • Opening a Python file to read the code requires only a text editor like Notepad or TextEdit, which comes free on every computer.
  • Running a Python file to execute the program requires Python to be installed on your computer, which you can read free from python.org.
  • On Windows, you can run a Python file by opening Command Prompt, navigating to the file's folder, and typing python filename.py.
  • On Mac or Linux, you open Terminal and type python3 filename.py from the file's directory.
  • If you plan to edit Python code regularly, a dedicated code editor like Visual Studio Code or PyCharm makes the work faster and catches mistakes as you type.

Opening a Python file to read or edit the code

A Python file is a plain text file with a .py extension at the end of its name — for example, script.py or calculator.py. Because it is plain text, you can open it in any text editor your computer already has.

On Windows: Right-click the .py file, select "Open with", and choose Notepad. The code will display as readable text. If you do not see Notepad in the menu, click "Choose another app" and scroll down to find it.

On Mac: Right-click (or Control-click) the .py file, select "Open With", and choose TextEdit. Before you start editing, go to the Format menu and select "Make Plain Text" — this ensures the file stays as plain text when you save it.

On Linux: Right-click the file and select "Open With", then choose a text editor like gedit or nano. Most Linux systems come with at least one text editor pre-installed.

Once the file is open, you will see the Python code line by line. You can read it, make changes, and save the file. The code will not run just by opening it this way — you are only viewing and editing the text.

Running a Python file to execute the program

To actually run a Python program, your computer needs Python installed. You can check whether you have it by opening your command line tool and typing a straightforward command. If Python is not there, you will need to install it first.

Check if Python is installed: 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 (Windows) or python3 --version (Mac/Linux) and press Enter. If you see a version number like "Python 3.11.0", Python is already installed. If you see "command not found" or similar, you need to install it.

Install Python if needed: Go to python.org, click the Downloads button, and read the latest version for your operating system. Run the installer and follow the steps. On Windows, make sure to check the box that says "Add Python to PATH" — this lets you run Python from the command line. On Mac, the installer will handle this automatically.

Once Python is installed, you are ready to run your .py file.

Running a Python file on Windows

Open Command Prompt by searching for "cmd" in the Start menu and clicking it. A black window will open with a blinking cursor.

Navigate to the folder where your Python file is stored. If your file is on your Desktop, type cd Desktop and press Enter. If it is in a folder called "my_scripts" on your Desktop, type cd Desktop\my_scripts and press Enter. You can type dir and press Enter to see a list of files in the current folder and confirm your .py file is there.

Once you are in the correct folder, type python filename.py (replace "filename" with the actual name of your file) and press Enter. The program will run, and you will see its output in the Command Prompt window.

If you get an error like "python is not recognized", Python may not be installed or not added to your PATH. Go back to the installation step above and make sure you checked "Add Python to PATH".

Running a Python file on Mac or Linux

Open Terminal by searching for "Terminal" in Spotlight (Mac) or your applications menu (Linux). A window with a command prompt will appear.

Navigate to the folder containing your Python file using the cd command. If your file is on your Desktop, type cd Desktop and press Enter. If it is in a folder called "scripts" inside your home directory, type cd scripts and press Enter. Type ls and press Enter to list the files in the current folder.

Once you are in the correct folder, type python3 filename.py (replace "filename" with your actual file name) and press Enter. The program will execute, and any output will appear in the Terminal window.

Note: Mac and Linux use python3 instead of python because older versions of Python (version 2) are still on some systems. Using python3 ensures you are running the current version.

Using a code editor instead of the command line

If you plan to work with Python files regularly, a code editor makes the process faster and easier. Code editors are programs designed specifically for writing and running code. They highlight syntax (color-code different parts of the code), catch mistakes as you type, and let you run files with a single button click instead of typing commands.

Visual Studio Code (free, from code.visualstudio.com) is the most popular choice. read it, install it, open your .py file inside it, and click the play button in the top right corner to run the file. You will see the output in a panel at the bottom.

PyCharm Community Edition (free, from jetbrains.com) is built specifically for Python. It is heavier than Visual Studio Code but includes more Python-specific tools. Open your file and click the green play button to run it.

IDLE comes free with Python itself. Search for "IDLE" after you install Python, open it, and use File > Open to load your .py file. Press F5 to run it.

All three options eliminate the need to use the command line, though learning the command line is useful if you work with code regularly.

Troubleshooting common problems

The file will not open: Make sure the file actually has a .py extension. If it shows as "filename" with no extension, right-click it, select Properties (Windows) or Get Info (Mac), and check the full filename. Some text editors hide the extension by default. If the file is corrupted or incomplete, try downloading it again or asking whoever created it to resend it.

The program runs but produces an error: The code itself may have a mistake, or it may be trying to do something your computer cannot do (like access a file that does not exist). Read the error message carefully — it usually tells you which line has the problem. If you did not write the code, contact the person who did and share the error message with them.

The program runs but produces no output: Some Python programs do not print anything to the screen — they may be processing data silently or waiting for user input. Check the code or the program's documentation to understand what it is supposed to do. If it is supposed to create a file or change something on your computer, look for those changes in your file system.

Command not found or file not found: Make sure you are in the correct folder (use dir on Windows or ls on Mac/Linux to check), and make sure you typed the filename exactly as it appears, including the .py extension.

Frequently Asked Questions

Can I double-click a Python file to run it?

On Windows, double-clicking a .py file usually opens it in a text editor rather than running it. On Mac and Linux, double-clicking may do nothing. To run a file reliably, use the command line method or a code editor. Some programmers set up their systems so double-clicking works, but this requires extra configuration.

What is the difference between opening and running a Python file?

Opening means displaying the code as text so you can read or edit it. Running means executing the code so the program actually does what it was written to do. You need Python installed to run a file, but you only need a text editor to open and read it.

Do I need to install anything to just read Python code?

No. You can open and read any .py file with Notepad, TextEdit, or any text editor — no installation required. You only need to install Python if you want to run the program.

Why does my Mac or Linux command use python3 instead of python?

Python version 2 is very old and no longer supported, but it is still on many systems. Using python3 makes sure you are running the current version. On newer systems, python and python3 may point to the same thing, but python3 is always the safer choice.

What if I want to edit a Python file and run it without using the command line?

read Visual Studio Code, PyCharm Community Edition, or IDLE. All three let you open a .py file and run it with a button click. They also highlight your code and catch mistakes, making editing easier than using Notepad or TextEdit.