如何运行安装的python脚本?

我用distutils来安装我的python包,用这个setup.py:

import distutils.core args = { 'name' : 'plugh', 'version' : '1.0', 'scripts' : [ "scripts/plugh" ], 'packages': [ "plugh" ], } d = distutils.core.setup( **args ) 

在Linux / Mac上,它按预期工作:

 % plugh hello world % 

Windows上,脚本“plugh”不运行:

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

我在http://bugs.python.org/issue7231上find了bug报告,当安装python时,\ Scripts目录没有被添加到PATH中,所以我应用了那张票据中描述的解决方法(即添加C:\ Python25 \ Scripts到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 

这是什么东西,只是不适用于Windows? 如果是这样,你应该如何在Windows机器上使用Python脚本?

我想我可以检测Windows,并添加一个额外的脚本列表,名为“plugh.bat”包含如下:

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

但是这真的是正确的答案吗? 我会想,用distutils包含的所有窗口定制,会有比这更好的答案。

Windows使用文件的扩展名来确定它将如何运行。

将您的文件plugh.py并在提示符下使用plugh.py来调用它。

  1. 如果您使用ActivePython ,则在安装过程中已经将C:\PythonXY\Scripts目录添加到%PATH% (ActivePython 2.6另外添加了PEP 370的%APPDATA%\Python\Scripts%PATH% )。

  2. 为了在Windows机器上部署脚本,最好使用Distribute ,它将负责为脚本安装.exe包装器, 调用安装包的实际Python(以避免与多个Python安装冲突 – 所以命名脚本结束。 py是不够的)。 有关此主题的更多信息,请阅读Distribute文档中的入口点