Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

How to Restore the Default Program for .py Files on Windows

.py files open in the wrong program, or nothing happens on double-click? Four ways to restore the Python file association on Windows 10 and 11.

3 min readBy Shailabh

Reviewed by the AmazingRDP technical team

Restoring the default program for .py files

Scripts that must run day and night belong on a server. See our Windows VPS plans. See Windows VPS from $8.99

On Windows, a double-click on a .py file should run it with Python. After installing an editor, choosing "Open with" once too often, or uninstalling an old Python version, the link can point somewhere else: scripts open in Notepad, in an editor, in the Microsoft Store, or do nothing. This link is called the file association. Here are four ways to restore it, mildest first.

What the default should be

The Python installer from python.org adds a small program called the Python Launcher (py.exe, in the Windows folder). It is the right default for .py files: it picks the correct Python version, even when several are installed.

1. "Open with"

  1. Right-click any .py file and choose Open with, Choose another app.
  2. Select Python from the list. If it is missing, scroll down, choose to look for an app on the PC, and select C:\Windows\py.exe.
  3. Click Always.

2. Settings

  1. Open Settings, Apps, Default apps.
  2. Type .py into the search field for file types.
  3. Click the current program and choose Python.

3. Repair the Python installation

If Python does not appear in those lists, its registration is damaged.

  1. Open Settings, Apps, Installed apps, find Python, and choose Modify. Or run the installer you downloaded again.
  2. Choose Modify, keep py launcher ticked, click Next, and tick Associate files with Python.
  3. Click Install.

If no Python is installed any more, install the current version from python.org with the same two boxes ticked. Upgrading the package tool afterwards: how to upgrade pip.

4. Command line and registry

When a previous choice refuses to go away, clear it by hand. In a Command Prompt as administrator:

assoc .py=Python.File
ftype Python.File="C:\Windows\py.exe" "%L" %*

That sets the system-wide association. A personal choice made earlier still overrides it. It is stored in the registry under your own user, in the key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice. Open Registry Editor (regedit), go to FileExts, then .py, right-click the sub-key UserChoice and choose Delete. Only that one sub-key: it holds nothing but your earlier "Open with" choice for this file type.

Sign out and in again, then use method 1 once to confirm Python as the default.

"It flashes and disappears"

That is not a broken association. The script ran in a console window, finished, and the window closed. To see the output:

  • Open a terminal in the folder and run py script.py, or
  • add input("Press Enter to close") as the script's last line.

Scripts without a window, such as programs with their own interface, use the ending .pyw, which belongs to pyw.exe.

Double-click opens the Microsoft Store

Windows ships placeholder files named python.exe that open the Store when no Python is installed. After installing Python from python.org, switch them off: Settings, Apps, Advanced app settings, App execution aliases, and turn off the two entries for Python.

Running a script by name alone

To type script.py in a terminal and have it run, the association above must be right. To type just script, add .PY to the environment variable PATHEXT, which the Python installer does when asked.

Do you want double-click to run or to edit?

Many people who write Python prefer the opposite default: double-click opens the editor, and scripts are run from a terminal. That is a legitimate choice, and it is safer, since a double-click on a script of unknown origin runs it with your rights. If your scripts "stopped running" after installing an editor, the editor took the association; decide which behaviour you want and set it with method 1.

Check the result

assoc .py
ftype Python.File
py --version

The first should answer .py=Python.File, the second should show py.exe, and the third the Python version the launcher will use.

The same question for PowerShell scripts is covered in making .ps1 files open with PowerShell. Scripts that must run on a schedule, whether or not your PC is on, belong on a server: see our Windows VPS plans.

Questions

How do I make .py files open with Python again?

Right-click a .py file, choose Open with, Choose another app, select Python (the launcher py.exe) and click Always.

Why does my Python script open and close immediately?

It ran and finished, and the console window closed. Run it from a terminal with py script.py to see the output.

What is the Python Launcher?

A small program, py.exe, installed with Python for Windows. It starts scripts with the right Python version.

Why does typing python open the Microsoft Store?

Windows has placeholder aliases for Python. Switch them off under App execution aliases after installing Python.