在Python中组合模块文件
有没有一种方法可以将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在寻找同一问题的解决方案后,我最终编写了一个简单的工具,它将多个 .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!
看看 Python Eggs:http://peak.telecommunity.com/DevCenter/PythonEggs
或者,您可以使用常规 zip: http://docs.python.org/library/zipimport.html< /a>
Take a look at Python Eggs: http://peak.telecommunity.com/DevCenter/PythonEggs
Or, you can use regular zips: http://docs.python.org/library/zipimport.html
最简单的方法是使用
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
. Ajar
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您可以创建包含 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.
请阅读此 PEP 了解信息。
另外,从 Zip 导入模块。
Read this PEP for informations.
Also, Import modules from Zip.
stickytape (提到
有时您只想重新分发一个
.py
文件,因此 PyInstaller、zipimport 和 Python Eggs 都不会削减那么这笔交易。PyBreeder(当前)抛出异常。
stickytape (mentioned before) worked for me.
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.