我可以使用 zipimport 来发送嵌入式 python 吗?

发布于 2024-09-08 17:45:05 字数 465 浏览 2 评论 0原文

目前,我正在使用我的应用程序部署完整的 python 发行版(原始 python 2.7 msi)。这是一个用delphi制作的嵌入式Web服务器。

阅读此内容,我想知道是否可能在我的应用程序中嵌入必要的 python 文件,以减少负载文件并避免与多个 python 版本发生冲突。

我以前有过 python for delphi 的经验,所以我只需要知道是否将 python dll + zip 与发行版 + 自己的脚本一起发送即可(如果存在我必须知道的任何警告或我可以查看的示例)

Currently, I'm deploying a full python distribution (the original python 2.7 msi) with my app. Which is an embedded web server made with delphi.

Reading this, I wonder if is possible to embed the necessary python files with my app, to decrease load files and avoid conflict with several python versions.

I have previous experience with python for delphi so I only need to know if only shipping the python dll + zip with the distro + own scripts will work (and if exist any caveats I must know or a sample where I can look)

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

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

发布评论

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

评论(2

2024-09-15 17:45:05

zipimport 应该适合你——我不熟悉 Delphi 的 Python,但我怀疑它会禁用该功能(嵌入应用程序可以做到这一点,但它是一个不寻常的选择)。请记住,您可以直接压缩并导入的是 Python 编码的模块(或者只是它们相应的 .pyc.pyo 字节代码)——DLL(即使重命名为 .pyds;-) 需要在磁盘上加载(因此,如果您有一个 zip 文件,则需要在应用程序启动时将其解压缩,例如解压缩到临时目录中)。

此外,您甚至不需要压缩所有模块,只需压缩那些您实际需要的模块(通过传递闭包)——并且您可以轻松地准确找出这些模块是,带有标准 Python 库的 modulefinder 模块。我刚才指出的文档页面上的示例应该可以澄清一些事情。快乐拉链!

zipimport should work just fine for you -- I'm not familiar with Python for Delphi, but I doubt it disables that functionality (an embedding application can do that, but it's an unusual choice). Just remember that what you can zip up and import directly are the Python-coded modules (or just their corresponding .pyc or .pyo byte codes) -- DLLs (even if renamed as .pyds;-) need to be on disk to be loaded (so if you have a zipfile with them it will need to be unzipped at the start of the app, e.g. into a temporary directory).

Moreover, you don't even need to zip up all modules, just those you actually need (by transitive closure) -- and you can easily find out exactly which modules those are, with the modulefinder module of the standard Python library. The example on the documentation page I just pointed to should clarify things. Happy zipping!

最好是你 2024-09-15 17:45:05

是的,这是可能的。

实际上,我正在使用 Zipimport 库在 Python 中编写自动化脚本。实际上,我在 zip 中包含了每个 .py 文件以及这些脚本所需的配置或 xml 文件。

然后,我调用一个针对 __main__.py 类的 .command 文件,该文件根据我的 sys.argv 参数重定向到所需的脚本,即真的很有用!

Yes it is possible.

I'm actually writing automatisation script in Python with the Zipimport library. I actually included every .py files in my zip as well as configuration or xml files needed by those script.

Then, I call a .command file targeting a __main__.py class that redirect towards the desired script according to my sys.argv parameters which is really useful!

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