Michael Yousrie
Michael Yousrie

Reputation: 1142

How to use argparse without passing 'python' in CMD?

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.

Upvotes: 1

Views: 191

Answers (1)

Michael Yousrie
Michael Yousrie

Reputation: 1142

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.

Upvotes: 1

Related Questions