1

I want to run a Python script, which uses argparse, without having to type python at the beginning.

What I do

$ python file.py --arg value

What I want

$ file --arg value

I have done the first part through putting the file in a folder that is added to PATH variable but then argparse is not working correctly as when I type in:

$ file --arg val

It says that arg can't be None even though I passed it in ( it can't read the args I pass it when called like this even though it works well when I use $ python file.py --arg val


I hope this makes sense.

Michael Yousrie
  • 1,132
  • 2
  • 10
  • 20

1 Answers1

1

So the solution to the problem was simple but very hard to get to.

The problem was something wrong in the registry.

Executing these 2 commands was the first step:

assoc .py=py_auto_file
ftype py_auto_file="C:\Anaconda\python.exe" "%1" %*

Then editing the registry value default located at key HKEY_CLASSES_ROOT\py_auto_file\shell\open\command to "C:\Python36\python.exe" "%1" %*

Then Add .PY to PATHEXT

NOTE

Replace Python36 with your version of python ( and the path too if that's different ).

Hope this helps people ;)


Thanks to @Lycopersicum who linked this Question in the comments below which helped me to get the answer.

Also This link helped me.

Community
  • 1
  • 1
Michael Yousrie
  • 1,132
  • 2
  • 10
  • 20
  • `py_auto_file` is wrong. At some point, either you or an IDE acting on your behalf modified the default file association, and Windows ended up using an automatically generated ProgId (program identifier). Among other things, with this you lose the shell drop handler. Reset back to using the default ProgID, `Python.File`, which should still be configured to use the py.exe launcher. You should be able to do this on the right-click "open with" menu. Select "choose another app". Select "Python" (it should have a rocket on the icon for the 'launcher') and "always use this app". – Eryk Sun Dec 07 '17 at 22:22
  • Also, it's not sensible that you're using CMD's `ftype` command to modify the value of "HKLM\Software\Classes\py_auto_file\shell\open\command", and then modifying it manually in the registry editor to a different value. Also, never edit HKCR; it's only meant to be a merged view on HKCU\Software\Classes and HKLM\Software\Classes, with precdence for HKCU. Editing it is indeterminate, depending on what keys already exist in HKCU and HKLM. – Eryk Sun Dec 07 '17 at 22:27
  • Also, `ftype` and `assoc` only modify the default HKLM associations and ProgIds. In general this has no effect on the user choice, if one exists, in "HKCU\Software\Microsoft\Windows\CurrentVersion\Exporer\FileExts". It seems to work sometimes if there's no current user choice and no HKCU defaults that override the HKLM defaults -- or if you're modifying the ProgId that's the current user choice. – Eryk Sun Dec 07 '17 at 22:31
  • I'm sorry but I don't know much of what you are saying. I had a problem and I got what I needed following those steps and I decided to share them in case someone needed them. – Michael Yousrie Dec 07 '17 at 23:46
  • And I thank you for that. Please feel free to edit my answer for it to include the correct solution using best practices. I'd love to help out as much as I have received help here. Thank you! – Michael Yousrie Dec 08 '17 at 05:53