Running a Python script from the "Run" prompt
I thought this would be easy, but I've been struggling for a while.
I have a Python script on my Windows machine called "myscript.py". I have it in a folder that's on my PATH, and I've added the ".py" extension to the PATHEXT environment variable, so I can run my script from a command prompt by just doing "myscript" (instead of typing "myscript.py"). This is working fine.
However, I cannot seem to get the same behavior when launching my script from the "Run" prompt (Windows-R). If I type "myscript.py" it works fine, but just doing "myscript" doesn't - I get the Windows cannot find 'myscript'...
error message.
I would have assumed that the "Run" prompt simply doesn't recognize the PATHEXT variable, but other types of programs seem to work - I can run .bat (Windows batch scripts) and .exe (Windows executables) from the Run prompt without any extension needed.
Why doesn't running my Python script without an extension work from the Run prompt?
Solution 1:
Doesn't really answer my question of why, but I did find a satisfactory workaround: I created a shortcut (right click on a blank area in a Windows Explorer window -> New -> Shortcut) and set the target of the shortcut to be my script. Once that shortcut is in a folder that's on my path, I can call my script with just the name of the shortcut (without any extension). This allows command line parameters to be passed to the Python script with no further modification.
Another alternative was provided by Anaksunaman - create a .bat/batch script wrapper around the script. This works fine unless your Python script needs to take params, in which case it will require more effort to pass along the parameters.
Solution 2:
Not an answer, and you likely already thought of this as a solution so this is really for others, but using a one line .bat
file to call the Python script works fine. And you can, of course, name the batch file myscript.bat
and it will work without the .bat
extension.