How to open a .py file
Python script · Source code
A .py file is a Python script, which is plain text. Any editor opens it. To actually run it you need Python installed, and then "python script.py" from a terminal.
What a .py file actually is
Python source is text, so nothing special is needed to read it. That is deliberate: the language was designed to be legible.
Running it is a separate matter, and the usual frustration is that Windows does not ship Python while macOS no longer ships the version most scripts expect.
Why it will not open
On Windows, double-clicking either does nothing or flashes a black window that vanishes. The window closing instantly is the script finishing, or crashing, faster than you can read the message.
Renaming it to an extension you can open will not help: changing the extension does not convert the file, it only changes what Windows tries to open it with.
Step by step
Windows
- 01To read or edit: right-click, choose "Open with", then VS Code or Notepad++.
- 02To run: install Python, ticking "Add Python to PATH" during setup, which is the step people miss.
- 03Open a terminal in the script folder and run: python script.py
- 04Run it from a terminal rather than double-clicking. That way the window stays open and you can read any error instead of watching it disappear.
- 05If it complains about a missing module, install it with: pip install <module name>
macOS
- 01Recent macOS no longer bundles Python. Install it with "brew install python" or from python.org.
- 02Run "python3 script.py" in Terminal. Note the 3: plain "python" may not exist.
What usually goes wrong
The window that disappears instantly
Double-clicking a .py on Windows opens a console, runs the script and closes it the moment it ends, including when it ends with an error. Running it from an already-open terminal keeps the output visible, which is the difference between guessing and knowing.
A script is a program
Python can delete files and make network connections just like any executable. Read a script before running it if you did not write it, which is easy precisely because it is plain text.
Programs that open .py
Our pick, in order. Every one links to the publisher's official download URL and the SHA-256 hash of the installer. Once you have downloaded it, you can check the file against our index before you run it.
If you end up downloading one of these from somewhere other than the publisher, what you can and cannot verify about that is worth two minutes.
- Sublime Text 4Sublime is a sophisticated cross-platform text editor for code, markup and prose.v4.0.0.420000 · 2 hashed installers92
- Notepad++Notepad++ is a free source code editor that supports several languages.v8.9.7 · 7 hashed installers95
- Microsoft Visual Studio CodeMicrosoft Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. Microsoft Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.v1.132.0 · 4 hashed installers100
Also registers .py in its manifest
Taken straight from publisher manifests. Some of these handle the format for a narrow purpose rather than as their main job.
Common questions
- How do I run a Python script on Windows?
- Install Python and tick "Add Python to PATH" during setup, then open a terminal in the script folder and run "python script.py". Running from a terminal rather than double-clicking keeps errors visible.
- Why does the black window close instantly?
- The script finished or crashed, and Windows closes the console as soon as it ends. Run it from a terminal you opened yourself so the output stays on screen.