5

I make a lot of python scripts - like blah.py, in my scripts directory, which is in the path. I want to just type "blah" to execute blah.py.

but when I do - the file opens in visual studio code?!

and I can't understand why - when I type

> ftype | grep -i python
Python.ArchiveFile="C:\WINDOWS\py.exe" "%L" %*
Python.CompiledFile="C:\WINDOWS\py.exe" "%L" %*
Python.File="C:\WINDOWS\py.exe" "%L" %*
Python.NoConArchiveFile="C:\WINDOWS\pyw.exe" "%L" %*
Python.NoConFile="C:\WINDOWS\pyw.exe" "%L" %*

and then

> assoc | grep -i .py
.py=Python.File
.pyc=Python.CompiledFile
.pyd=Python.Extension
.pyo=Python.CompiledFile
.pyw=Python.NoConFile
.pyz=Python.ArchiveFile
.pyzw=Python.NoConArchiveFile

so by my reading .py should be a Python.File, which should run C:\windows\py.exe.

any ideas? So far I'm having to make a .bat file corresponding to every single .py file - that just runs it :(

4
  • 2
    You are showing linux commands. Are you running this from straight cmd.exe or using bash from a sub linux os on the system? I would however advise against associating files directly with an interpreter, it is a security risk. Instead add py.exe to path and simply run them as py blah.py
    – Gerhard
    Commented Sep 7, 2023 at 4:49
  • 1
    I'm running this from straight command - but I have spent a lot of my life in unix - so I do use unix tools. as for the security risk - I get that, but the convenience is worth it. py blah.py is not an option a) it doesn't magically work across the path, and b) when I write tools, I write them in lots of things, python is just one of them, I don't want to have to remember "was this a shell script? a batc file? a python file? an exe?"... Commented Sep 7, 2023 at 4:56
  • Are you familiar with Regedit, or does it scare you? Commented Sep 11, 2023 at 6:18
  • You may edit the extension association in the explorer, see 4 Ways to Open File Properties in Windows 10, and [Change...] the Opens with value there.
    – Wolf
    Commented Mar 19 at 12:28

2 Answers 2

2
+50

Given that you're most familiar with Unix, I'm not sure if you are comfortable with Regedit and the Windows registry.

So let's get you a simple well-regarded tool to handle this task without asking you to manually edit the Windows registry.

First download FileTypesMan from NirSoft. It's written by Nir Sofer, who is a well-known and trusted developer. It's not open-source (unfortunately), but Nir has been publishing free tools for over 15 years with an unblemished record.

Now follow these simple steps within FileTypesMan:

  1. Find the .py file type in the top pane and click on it.
  2. In the bottom pane, find the open action (Name column) and double-click it.
  3. In the ensuing dialog box, change the command line to "C:\Windows\py.exe" "%1"
  4. In that same dialog box, ensure the Default Action checkbox is checked and the Disabled checkbox is not checked, then click OK to close the dialog box.

Voilà. You're done.

You can now close FileTypesMan.

Depending on the installed version of Windows, you may need to restart the shell. If you don't know how to do that programmatically, just log off, and log back in. Or, if you prefer, reboot.

As Gerhard mentioned in the comments, there is a significant security risk to allowing scripts to run directly like this, but you mentioned you understand the risks and the tradeoffs are worth it to you. I simply recap those comments here in this answer to help people in the future in the event that the comments get deleted.

1
  • thanks - I renamed hklm\software\classes\.py and hkcu\software\classes\.py and without them there - the ftype and assoc seem to work Commented Sep 12, 2023 at 0:23
1

...I want to just type "blah" to execute blah.py
- You can try:

1. Add the path to the Python.exe binary

2. Add eXtensions .py and .pyw to the PathExt variable

3. Run your files

> Your_py_Script
> Your_pyw_Script

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .