在Python中组合模块文件

发布于 2024-07-26 01:54:41 字数 96 浏览 5 评论 0原文

有没有一种方法可以将 Python 文件组合在一起,类似于 Java 中的 JAR? 我需要一种打包 Python 类和函数集的方法,但与标准模块不同,我希望它位于一个文件中。

Is there a way to put together Python files, akin to JAR in Java? I need a way of packaging set of Python classes and functions, but unlike a standard module, I'd like it to be in one file.

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

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

发布评论

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

评论(6

π浅易 2024-08-02 01:54:41

在寻找同一问题的解决方案后,我最终编写了一个简单的工具,它将多个 .py 文件合并为一个: PyBreeder

它仅适用于纯 Python 模块,并且可能需要一些反复试验才能获得正确的模块顺序,但每当您想要部署具有某些依赖项的脚本作为单个.py。 非常欢迎评论/补丁/批评!

After looking for a solution to the same problem, I ended up writing a simple tool which combines multiple .py files into one: PyBreeder

It will only work with pure-Python modules and may require some trial-and-error to get the order of modules right, but it is quite handy whenever you want to deploy a script with some dependencies as a single .py. Comments/patches/critique are very welcome!

流殇 2024-08-02 01:54:41

最简单的方法是使用zip。 Java 中的 jar 文件是一个 zip 文件,其中包含一些元数据,例如清单; 但您不一定需要元数据 - 只要您将该 zip 文件放在 sys.path 上,Python 就可以从 zip 文件内部导入,就像您对任何目录所做的那样。 在 zip 文件中,您可以拥有源代码(.py 文件),但是每次进程首次导入它们时,Python 都必须动态编译它们; 或者您可以拥有字节码文件(.pyc 或 .pyo),但随后您将受限于特定版本的 Python 以及缺少(对于 .pyc)或存在(对于 .pyo)标志 -O (或 -OO) )。

正如其他答案所指出的,有一些格式,例如 .egg 也可以用 Python 中的元数据来丰富 zip 文件,就像 Java .jar 一样,但无论是在特定的用例中为您提供额外的价值,普通 zip 文件是您的决定

The simplest approach is to just use zip. A jar file in Java is a zipfile containing some metadata such as a manifest; but you don't necessarily need the metatada -- Python can import from inside a zipfile as long as you place that zipfile on sys.path, just as you would do for any directory. In the zipfile you can have the sources (.py files), but then Python will have to compile them on the fly each time a process first imports them; or you can have the bytecode files (.pyc or .pyo) but then you're limited to a specific release of Python and to either absence (for .pyc) or presence (for .pyo) of flag -O (or -OO).

As other answers indicated, there are formats such as .egg that enrich the zipfile with metatada in Python as well, like Java .jar, but whether in a particular use case that gives you extra value wrt a plain zipfile is a decision for you to make

心欲静而疯不止 2024-08-02 01:54:41

您可以创建包含 Python 代码的 zip 文件,并使用 zipimport 从 zip 文件导入。 诸如 PyInstaller(跨平台)或 py2exe (Windows) 将为您完成这一切。

You can create zip files containing Python code and import from zip files using zipimport. A system such as PyInstaller (cross-platform) or py2exe (Windows) will do all this for you.

半葬歌 2024-08-02 01:54:41

请阅读此 PEP 了解信息。
另外,从 Zip 导入模块

Read this PEP for informations.
Also, Import modules from Zip.

避讳 2024-08-02 01:54:41

stickytape (提到

stickytape importingSomeModules.py > resultWithEmbeddedImports.py

有时您只想重新分发一个 .py 文件,因此 PyInstallerzipimportPython Eggs 都不会削减那么这笔交易。

PyBreeder(当前)抛出异常。

stickytape (mentioned before) worked for me.

stickytape importingSomeModules.py > resultWithEmbeddedImports.py

Sometimes you just want to redistribute just a single .py file, so neither PyInstaller, zipimport nor Python Eggs cut the deal then.

PyBreeder is (currently) throwing exceptions.

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