【发布时间】:2012-04-16 21:18:23
【问题描述】:
我创建了一个不错的 python Twisted 应用程序,其中包含一个用于 twistd runner 的插件,如 Twisted 文档中所述:http://twistedmatrix.com/documents/current/core/howto/tap.html。我在使用 PyInstaller 打包时遇到问题:在执行冻结的应用程序期间找不到我的 twistd 插件。
为了发布我的项目,我使用 twistd runner 模块创建了自己的顶级启动脚本,例如
#!/usr/bin/env python
from twisted.scripts.twistd import run
from sys import argv
argv[1:] = [
'--pidfile', '/var/run/myapp.pid',
'--logfile', '/var/run/myapp.log',
'myapp_plugin'
]
run()
接下来,我使用 PyInstaller 将其冻结为单个目录部署。执行上面的冻结脚本失败,因为它找不到我的 twistd 插件(为简洁而编辑):
~/pyinstall/dist/bin/mystartup?16632/twisted/python/modules.py:758:
UserWarning: ~/pyinstall/dist/mystartup?16632 (for module twisted.plugins)
not in path importer cache (PEP 302 violation - check your local configuration).
~/pyinstall/dist/bin/mystartup: Unknown command: myapp_plugin
通常,Twistd 检查 Python 系统路径以发现我在 twisted/plugins/myapp_plugin.py 中的插件。如果我在启动脚本中打印扭曲插件列表,则该列表在 PyInstaller 生成的可执行文件中为空,例如
from twisted.plugin import IPlugin, getPlugins
plugins = list(getPlugins(IPlugin))
print "Twistd plugins=%s" % plugins
我使用了一个有些默认的 PyInstaller 规范文件,没有指定隐藏的导入或导入钩子。
我喜欢twistd 带有日志记录、pid 文件等的功能,所以我想避免完全放弃twistd runner 来规避插件问题。 有没有办法确保在冻结的可执行文件中找到我的 twistd 插件?
【问题讨论】:
-
好问题。谢谢你问。打包 Twisted 插件是一个痛点 - 打包工具通常不会考虑到它 - 我焦急地等待更了解 pyinstaller 的人的回答:)。
-
谢谢。与此同时,我设法在一个完全不使用twistd而只是启动twisted reactor的twisted服务器python脚本上成功运行了PyInstaller,例如
reactor.listenTCP(9999, site); reactor.run()。所以一般来说twisted没有打包问题,只有twistd插件系统。 -
警告中的路径
~/pyinstall/dist/mystartup?16632看起来很可疑。我认为它是 pyinstaller 创建的——因此 pyinstaller 可能负责将其添加到 Python 导入配置的正确部分(例如,但不限于,sys.path)。我真正想知道的是为什么路径中有~。您可能知道,除非使用os.path.expanduser明确解释,否则该字符没有特殊意义。twisted.plugins执行的路径操作都会被它搞糊涂。带有文字~的该路径是否出现在导入配置中的某处? -
啊,很抱歉让您感到困惑,我手动编辑了错误并将 /home/martijn 替换为 '~',我的错
标签: python twisted pyinstaller twistd