9

I used distutils to install my python package, with this setup.py :

import distutils.core

args = {
    'name' :            'plugh',
    'version' :         '1.0',
    'scripts' :         [ "scripts/plugh" ],
    'packages':         [ "plugh" ],
}

d = distutils.core.setup(
    **args
)

On linux/mac, it works as expected:

% plugh
hello world
% 

On windows, the script "plugh" does not run:

C:\Python25\Scripts>plugh
'plugh' is not recognized as an internal or external command,
operable program or batch file.

C:\Python25\Scripts>

I found the bug report at http://bugs.python.org/issue7231 that the \Scripts directory is not added to PATH when you install python, so I applied the workaround described in that ticket (i.e. add C:\Python25\Scripts to PATH)

C:\Python25\Scripts>path
PATH=c:\Python25\Scripts;C:\Program Files\Legato\nsr\bin;C:\WINDOWS\system32;C:\
WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\QuickTime\QTSystem\;c:\python2
5;c:\local;C:\WINDOWS\system32\WindowsPowerShell\v1.0

Is this something that just doesn't work on Windows? And if so, how exactly are you supposed to use python scripts on a windows machine?

I suppose that I could detect Windows, and add an additional script to the list, called "plugh.bat" containing something like:

@echo off
c:\python25\python.exec c:\python25\scripts\plugh %1 %2 %3 %4 %5 %6 %7 %8 %9

but is that really the right answer here? I would have thought that with all the customizations that distutils contains for windows, there would be a better answer than that.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
Mark S.
  • 91
  • 1
  • 2
  • 2
    BTW, the parameters sequence %1 %2 %3 %4 %5 %6 %7 %8 %9 could be replaced with %* if you just need to pass all batch input to pyhton script (or any other executable). – Alex Musayev Jun 03 '12 at 13:33

2 Answers2

6

windows uses the extension of the file to determine how it will run.

Name your file plugh.py and use plugh.py on the prompt to call it.

nosklo
  • 217,122
  • 57
  • 293
  • 297
  • In case you want the user to click on the `.py` file, you can associate `.py` files with pythonw.exe instead of python.exe so that the terminal does not stay open: http://docs.python.org/using/windows.html#executing-scripts – John Paulett Dec 02 '09 at 01:25
5
  1. If you use ActivePython, it will already add the C:\PythonXY\Scripts directory to your %PATH% (ActivePython 2.6 additionally adds PEP 370's %APPDATA%\Python\Scripts to %PATH%) during the installation.

  2. For deploying scripts on Windows machine, better use Distribute which will take care of installing .exe wrappers for your scripts and invoking the actual Python with which your package was installed (to avoid conflict with multiple Python installations -- so naming your script to end .py is just not enough). For more on this topic, read about entry points in Distribute documentation.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187