1

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?

0

2 Answers 2

2

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.

1
  • +1 for enabling simple parameters with a shortcut. Commented Jul 2, 2015 at 6:08
1

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.

1
  • I had thought of that, but my Python script takes arguments/command line params, and I don't really want to have to write batch to pass any arguments along to it. I did find a way to do it though - see my answer! Commented Jun 30, 2015 at 11:50

You must log in to answer this question.

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