distutils - 如果窗口如何添加.pyw脚本

时间:2013-02-14 20:59:02

标签: python python-2.7 distutils

我正在分发一个简单的库/应用程序,其中包含一个带GUI的脚本。在Windows下我想让它由pythonw.exe运行,最好是把它变成.pyw文件。

root/
    lib/
        lib.py

    guiscript.py
    setup.py

我希望用户能够在任何路径中安装guiscript

我从this question:

偷了这个钩子
from distutils.core import setup
from distutils.command.install import install
import os, sys

class my_install(install):
    def run(self):
        install.run(self)
        try:
            if (sys.platform == "win32") and sys.argv[1] != "-remove":
                os.rename("guiscript.py",
                          "guiscript.pyw")
        except IndexError:pass

setup(...
      cmdclass={"install": my_install})

但这不起作用,因为它更改了源文件夹中guiscript.py的名称,因为该路径是相对于setup.py的。

是否有合理的方法来获取脚本安装路径,或者另外一种简单的方法来查找guiscript.py(它没有在PYTHONPATH中给出它。)


因为我没有50个业力,所以我不能在7个小时内回复自己的帖子,但这里是:

好的,我找到了解决方案。如果你愿意,我可以删除这个问题,但是现在我会保留它以防其他人有同样的问题。

from distutils.core import setup
from distutils.command.install_scripts import install_scripts
import os, sys

class my_install(install_scripts):
    """Change main script to .pyw after installation.

    If sys.argv == '-remove'; it's ran as uninstall-script.
    Override run() and then call parent."""
    def run(self):
        install_scripts.run(self)
        try:
            if (sys.platform == "win32") and sys.argv[1] != "-remove":
                for script in self.get_outputs():
                    if script.endswith("guiscript.py"):
                        os.rename(script, script+"w")
        except IndexError:pass

setup(...
      cmdclass={"install_scripts": my_install}
     )

0 个答案:

没有答案