Windows does not recognize .py file extension as a Python File

So whenever I save a file with the .py file extension, Windows doesn’t show the file type as “Python File” like it should, and the icon is just a blank paper, unlike normally where it will show a paper with lines and the Python logo at the bottom right corner.
It just says “PY File” in File Type, and whenever I click on it to run, it always asks me what application I want to use to open it. Then when I choose Python it opens Python for a quick second before abruptly closing/crashing. Please help because I need to use Python.

Select the box to always use the app to open .py files. If you installed the py launcher, make sure the “Python” app that you select has a rocket and the Python logo on the icon.

Test a script that just contains input('press enter to exit').

“py.exe” and “python.exe” are console applications, which allocate a console if they don’t inherit one. In this case, when Python exits, no process is attached to the console anymore, so it gets immediately destroyed. This is particularly annoying if Python exits abnormally due to an unhandled exception or crash. To avoid this problem, run the script from a command-line shell such as CMD or PowerShell. In this case Python will inherit its console from the shell, and the shell keeps the console alive after Python exits.

In my case, there is no box that says always use this app, which is exactly what my problem is. I’ve tried simple and complex files, but it doesn’t work.

My method:

  1. Open notepad and put the code in
  2. save as .py file

Am I doing it wrong? is there any video tutorial to this?

I believe that the problem here might be Notepad.

I am not a regular Windows user, but on the few times I have tried it,
if you name a file “script.py” in Notepad, it actually names it
“script.py.txt” and the file will be considered to be a text file. I
think. It’s been a long time since I tried it, and maybe I am
misremembering.

So start by ignoring Notepad and use a better editor designed for
programmers.

Or you might want to consider using an IDE:

I don’t recall when the behavior changed, but Notepad in Windows 11 doesn’t add the “.txt” extension if the filename already has an extension that’s registered (i.e. defined in the registry), even if the “Save as type” is “Text Documents”. The latter used to be the culprit for adding the “.txt” extension because people would forget to change the type to “All Files”. However, Notepad still adds “.txt” for “Text Documents” if the filename extension is unknown.

Modify Explorer’s settings to make sure that it doesn’t hide file extensions for known file types. This will ensure that Explorer doesn’t show “script.py.txt” as “script.py”. This tops my list of annoying features that are enabled by default in Explorer.

Right-click a .py file to get the context menu, and from “open with”, select “choose another app”. This dialog should have a box to select “always use this app to open .py files”.

This is all basic information that I am already well aware of, and I have done all this. But my issue is this:

My computer recognizes a .py file as a “PY File” and not “Python File”, and whenever I try opening a .py file, it opens for 1 second then closes. And I am sure that the issue is not the extension, because I know that it would show “Text Document”

Did you try this? What happened?

I’m curious why assigning the file association seems to be ignored. If you don’t mind, please run the following command in the shell, and reply with the output pasted in a preformatted text block (e.g. press Ctrl+E and then Ctrl+V).

reg.exe query HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py /s

Example output:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\OpenWithList
    MRUList    REG_SZ    a
    a    REG_SZ    py.exe

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\OpenWithProgids
    Python.File    REG_NONE

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice
    ProgId    REG_SZ    Python.File
    Hash    REG_SZ    mWzjDh6X9Th=

SOLUTION - file saving as .txt and not as .py

  1. To open File Explorer right away, press Windows + E.
  2. Select the Show option under the View tab.
  3. Click on File Name Extensions.
  4. This will allow file extensions to be used in Windows 11.

This will show that the file names is not “hello.py” but “hello.py.txt”.
5. Rename the file by deleting “txt”.

Now the warning will pop - up and the file image will change to a Python one.

Hope this helps!

1 Like

image

how to fix it?

Settings → Apps → Default Apps → Choose default apps by file type

image

You need to write the command as python file_name.py.txt, I know this is not the best solution but it still works on my windows 10 pc.