为 python 独立可执行文件创建插件

发布于 2024-07-08 13:21:52 字数 321 浏览 7 评论 0原文

如何为使用 pyInstaller、py2exe 或类似工具创建的独立可执行文件创建一个好的插件引擎?

我没有使用 py2exe 的经验,但 pyInstaller 使用导入钩子从其压缩存储库导入包。 当然,我可以动态导入另一个用 pyInstaller 创建的压缩存储库并执行代码 - 这可能是一个简单的插件引擎。

当插件(这是动态导入的)使用原始存储库中不存在的库(从未导入)时,就会出现问题。 这是因为导入挂钩适用于原始应用程序,并在原始存储库中搜索包 - 而不是后来导入的包(插件包存储库)。

有没有简单的方法来解决这个问题? 也许存在这样的引擎?

how to create a good plugin engine for standalone executables created with pyInstaller, py2exe or similar tools?

I do not have experience with py2exe, but pyInstaller uses an import hook to import packages from it's compressed repository. Of course I am able to import dynamically another compressed repository created with pyInstaller and execute the code - this may be a simple plugin engine.

Problems appears when the plugin (this what is imported dynamically) uses a library that is not present in original repository (never imported). This is because import hook is for the original application and searches for packages in original repository - not the one imported later (plugin package repository).

Is there an easy way to solve this problem? Maybe there exist such engine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热鲨 2024-07-15 13:21:52

当编译成exe时,你会遇到这个问题。

我能想到的允许用户使用他们的插件访问以使用任何 python 库的唯一选择是将所有库包含在 exe 包中。

将支持的库限制为一个子集,并将其列在文档中可能是个好主意。 由你决定。

我只用过py2exe。

在 py2exe 中,您可以指定在 setup.py 文件中搜索中未找到的库。

这是一个示例:

from distutils.core import setup
import py2exe

setup (name = "script2compile",
       console=['script2compile.pyw'],
       version = "1.4",
       author = "me",
       author_email="[email protected]",
       url="myurl.com",
       windows = [{
                    "script":"script2compile.pyw",
                    "icon_resources":[(1,"./ICONS/app.ico")]  # Icon file to use for display
                 }],
       # put packages/libraries to include in the "packages" list
       options = {"py2exe":{"packages": [   "pickle",
                                            "csv",
                                            "Tkconstants",
                                            "Tkinter",
                                            "tkFileDialog",
                                            "pyexpat",
                                            "xml.dom.minidom",
                                            "win32pdh",
                                            "win32pdhutil",
                                            "win32api",
                                            "win32con",
                                            "subprocess", 
                                        ]}} 

       )

import win32pdh
import win32pdhutil
import win32api

When compiling to exe, your going to have this issue.

The only option I can think of to allow users access with thier plugins to use any python library is to include all libraries in the exe package.

It's probably a good idea to limit supported libraries to a subset, and list it in your documentation. Up to you.

I've only used py2exe.

In py2exe you can specify libraries that were not found in the search in the setup.py file.

Here's a sample:

from distutils.core import setup
import py2exe

setup (name = "script2compile",
       console=['script2compile.pyw'],
       version = "1.4",
       author = "me",
       author_email="[email protected]",
       url="myurl.com",
       windows = [{
                    "script":"script2compile.pyw",
                    "icon_resources":[(1,"./ICONS/app.ico")]  # Icon file to use for display
                 }],
       # put packages/libraries to include in the "packages" list
       options = {"py2exe":{"packages": [   "pickle",
                                            "csv",
                                            "Tkconstants",
                                            "Tkinter",
                                            "tkFileDialog",
                                            "pyexpat",
                                            "xml.dom.minidom",
                                            "win32pdh",
                                            "win32pdhutil",
                                            "win32api",
                                            "win32con",
                                            "subprocess", 
                                        ]}} 

       )

import win32pdh
import win32pdhutil
import win32api
胡大本事 2024-07-15 13:21:52

PyInstaller 确实有一个用于处理隐藏导入的插件系统,并且附带了其中几个已经存在的插件系统。请参阅网页 (http://www.pyinstaller.org) 上面写着:

PyInstaller 的主要目标是与开箱即用的第 3 方软件包兼容。 这意味着,使用 PyInstaller,使外部包工作所需的所有技巧都已集成在 PyInstaller 本身中,因此不需要用户干预。 您永远不需要在 wiki 中寻找技巧并对您的文件或安装脚本应用自定义修改。 检查我们的 SupportedPackages 兼容性列表。

PyInstaller does have a plugin system for handling hidden imports, and ships with several of those already in. See the webpage (http://www.pyinstaller.org) which says:

The main goal of PyInstaller is to be compatible with 3rd-party packages out-of-the-box. This means that, with PyInstaller, all the required tricks to make external packages work are already integrated within PyInstaller itself so that there is no user intervention required. You'll never be required to look for tricks in wikis and apply custom modification to your files or your setup scripts. Check our compatibility list of SupportedPackages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文