相当于 Java .jar 文件的 python 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25455978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the python equivalent to a Java .jar file?
提问by Nathan
Java has the concept of packaging all of the code into a file called a Jar file. Does Python have an equivalent idea? If so, what is it? How do I package the files?
Java 的概念是将所有代码打包到一个称为 Jar 文件的文件中。Python 有类似的想法吗?如果是,那是什么?我如何打包文件?
采纳答案by abarnert
Python doesn't have any exact equivalent to a .jarfile.
Python 没有任何完全等同于.jar文件的内容。
There are many differences, and without knowing exactly what you want to do, it's hard to explain how to do it. But the Python Packaging User Guidedoes a pretty good job of explaining just about everything relevant.
有很多不同之处,在不确切知道您想要做什么的情况下,很难解释如何去做。但是Python Packaging User Guide很好地解释了几乎所有相关的内容。
Here are some of the major differences.
以下是一些主要差异。
A .jar file is a compiled collection of classes that can be dropped into your application, or installed anywhere on your CLASSPATH.
.jar 文件是编译好的类集合,可以放入应用程序中,或安装在 .jar 文件中的任何位置CLASSPATH。
In Python:
在 Python 中:
- A
.py(or.pyc) module can be dropped into your application, or installed anywhere on yoursys.path, and it can be imported and used. - A directory full of modules can be treated the same way; it becomes a package (or, if it doesn't contain an
__init__.py, it merges with other directories of the same name elsewhere onsys.pathinto a single package). - A
.ziparchive containing any number of modules and packages can be stored anywhere, and its path added to yoursys.path(e.g., at runtime or viaPYTHONPATH) and all of its contents become importable.
- 一个
.py(或.pyc)模块可以放入你的应用程序中,或者安装在你的任何地方sys.path,并且可以被导入和使用。 - 一个充满模块的目录可以用同样的方式处理;它成为一个包(或者,如果它不包含
__init__.py,则它与其他地方的其他同名目录合并sys.path为一个包)。 - 一个
.zip包含任何数量的模块和包的归档可以在任何地方进行存储,并添加到您的路径sys.path(例如,在运行时或通过PYTHONPATH)和它的所有内容将成为可导入。
Most commonly, you want things to be installed into a system, user, or virtualenv site-packagesdirectory. The recommended way to do that is to create a pip-compatible package distribution; people then install it (and possibly automatically download it from PyPI or a private repo) via pip.
最常见的是,您希望将东西安装到系统、用户或 virtualenvsite-packages目录中。推荐的方法是创建一个pip兼容的包分发;人们然后通过pip.
pipdoes a lot more than that, however. It also allows you to manage dependencies between packages. So ideally, instead of listing a bunch of prereqs that someone has to go download and install manually, you just make them dependencies, and someone just has to pip install your-library. And it keeps track of the state of your site-packages, so you can uninstall or upgrade a package without having to track down the specific files.
pip然而,做的远不止这些。它还允许您管理包之间的依赖关系。因此,理想情况下,与其列出一堆必须有人手动下载和安装的先决条件,您只需让它们成为依赖项,而有人只需要pip install your-library. 它会跟踪您的站点包的状态,因此您可以卸载或升级包而无需跟踪特定文件。
Meanwhile, in Java, most .jarfiles are cross-platform; build once, run anywhere. A few packages have JNI native code and can't be used this way, but it's not the norm.
同时,在 Java 中,大多数.jar文件是跨平台的;一次构建,随处运行。一些包有 JNI 本机代码,不能这样使用,但这不是常态。
In Python, many packages have C extensions that have to be compiled for each platform, and even pure-Python packages often need to do some install-time configuration. And meanwhile, "compiling" pure Python doesn't do anything that can't be done just as well at runtime. So in Python, you generally distribute sourcepackages, not compiledpackages.
在 Python 中,许多包都有 C 扩展,必须为每个平台编译,甚至纯 Python 包也经常需要做一些安装时配置。同时,“编译”纯 Python 不会做任何在运行时无法完成的事情。所以在 Python 中,你通常分发源包,而不是编译包。
However, .wheelis a binary package format. You can pip wheelto build binary packages for different targets from the source package; then, if someone tries to pip installyour package, if there's a wheel for his system, that will be downloaded and installed.
但是,.wheel是二进制包格式。您可以pip wheel从源包中为不同的目标构建二进制包;然后,如果有人尝试使用pip install您的软件包,如果他的系统有一个轮子,就会下载并安装。
回答by Alexander Gessler
Easy Install from setup_toolsdefines the .eggformat for deploying Python libraries or applications. While similar to JAR, it is nowhere spread as universally as JARs in Java world. Many people just deploy the .pyfiles.
Easy Install fromsetup_tools定义了.egg部署 Python 库或应用程序的格式。虽然与 JAR 相似,但它在 Java 世界中的普及程度不如 JAR。许多人只是部署.py文件。
A newer format, intended to supersede eggs, is wheel.
一种旨在取代鸡蛋的较新格式是wheel。
回答by Federico Simonetta
Though it's not a perfect susbstitute of jar due to portability issues, I would add the "auto-extracting" archive way.
尽管由于可移植性问题它不是 jar 的完美替代品,但我会添加“自动提取”存档方式。
One possibility is "makeself": https://makeself.io/
一种可能性是“makeself”:https://makeself.io/
But if you don't need to package external files, and if you like KISS approach, the following is a nice and clean alternative:
但是如果您不需要打包外部文件,并且如果您喜欢 KISS 方法,那么以下是一个不错的选择:
The following is taken from Asim Jalis's website.
以下内容摘自 Asim Jalis 的网站。
How to deploy a Python application as a zip file
如何将 Python 应用程序部署为 zip 文件
Create a file __main__.pycontaining:
创建一个__main__.py包含以下内容的文件:
print "Hello world from Python"
Zip up the Python files (in this case just this one file) into app.zipby typing:
app.zip通过键入以下内容将 Python 文件(在本例中仅此一个文件)压缩到:
zip app.zip *
The next step adds a shebangto the zip file and saves it as app—at this point the file appis a zip file containing all your Python sources.
下一步向 zip 文件添加shebang并将其另存为app——此时该文件app是一个包含所有 Python 源代码的 zip 文件。
echo '#!/usr/bin/env python' | cat - app.zip > app
chmod 755 app
That's it. The file appis now have a zipped Python application that is ready to deploy as a single file.
就是这样。该文件app现在有一个已准备好部署为单个文件的压缩 Python 应用程序。
You can run appeither using a Python interpreter as:
您可以app使用 Python 解释器运行:
python app
Or you can run it directly from the command line:
或者您可以直接从命令行运行它:
./app
Reference: https://gist.github.com/asimjalis/4237534

