python – 使用pyinstaller打包twistd插件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 使用pyinstaller打包twistd插件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2374字,纯文字阅读大概需要4分钟。
内容图文
![python – 使用pyinstaller打包twistd插件](/upload/InfoBanner/zyjiaocheng/777/8ed9efb01bbc4f4dad11ba93139c20a0.jpg)
我创建了一个很好的python Twisted应用程序,带有一个扭曲的运行程序的插件,如Twisted Documentation:http://twistedmatrix.com/documents/current/core/howto/tap.html中所指定的.我在使用PyInstaller打包时遇到问题:在执行冻结的应用程序期间找不到我的扭曲插件.
为了运送我的项目,我使用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将其冻结为单个目录部署.执行上面的冻结脚本失败,因为它找不到我的扭曲插件(为简洁起见编辑):
~/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中发现我的插件.如果我在启动脚本中打印twistd插件列表,则列表在PyInstaller生成的可执行文件中为空,例如
from twisted.plugin import IPlugin, getPlugins
plugins = list(getPlugins(IPlugin))
print "Twistd plugins=%s" % plugins
我使用了一个默认的PyInstaller spec文件,没有指定隐藏的导入或导入钩子.
我喜欢使用日志,pid文件等扭曲的功能,所以我想避免完全放弃扭曲的跑步者来规避插件问题.
有没有办法确保在冻结的可执行文件中找到我的扭曲插件?
解决方法:
我通过逆向工程找到了一些扭曲的代码.在这里我硬编码插件导入.对我来说这适用于PyInstaller.
#!/usr/bin/env python
import sys
from twisted.application import app
from twisted.scripts.twistd import runApp, ServerOptions
import myapp_plugin as myplugin
plug = myplugin.serviceMaker
class MyServerOptions(ServerOptions):
"""
See twisted.application.app.ServerOptions.subCommands().
Override to specify a single plugin subcommand and load the plugin
explictly.
"""
def subCommands(self):
self.loadedPlugins = {plug.tapname:plug}
yield (plug.tapname,
None,
# Avoid resolving the options attribute right away, in case
# it's a property with a non-trivial getter (eg, one which
# imports modules).
lambda plug=plug: plug.options(),
plug.description)
subCommands = property(subCommands)
def run():
"""
Replace twisted.application.app.run()
To use our ServerOptions.
"""
app.run(runApp, MyServerOptions)
sys.argv[1:] = [
'--pidfile', '/var/run/myapp.pid',
'--logfile', '/var/run/myapp.log',
plug.tapname] + sys.argv[1:]
run()
内容总结
以上是互联网集市为您收集整理的python – 使用pyinstaller打包twistd插件全部内容,希望文章能够帮你解决python – 使用pyinstaller打包twistd插件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。