如何创建Python Egg文件
我对 Python 中的 Egg 文件有疑问。
我有很多按包组织的Python代码,我正在尝试创建egg文件。 我遵循说明,但它们很常见。
据此,看来我需要有一个 setup.py 文件。
- 您能告诉我需要在 setup.py 文件中放入什么内容以及它应该驻留在哪里吗?
- 我想创建 setup.py 然后启动“setup.py bdist_egg”来获取 Egg 文件就足够了。您能确认一下吗?
- 是否可以仅将 .pyc 文件包含到 Egg 文件中?
- 拥有 .egg 文件,我如何可以从中启动代码,而无需像
java -jar
那样解压?
I have questions about egg files in Python.
I have much Python code organized by package and I'm trying to create egg files.
I'm following instructions, but they are very common.
According to that, it seems I need to have a setup.py file.
- Would you please tell me what I need to put into setup.py file and where it should reside?
- I suppose it's enough to create setup.py and then start "setup.py bdist_egg" for getting egg file. Could you please confirm?
- Is it possible to include only .pyc files into egg file?
- Having .egg file how I can just start the code from it without unpacking like
java -jar <jar file>
does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在阅读错误的文档。您想要这个: https ://setuptools.readthedocs.io/en/latest/setuptools.html#develop-deploy-the-project-source-in-development-mode
创建 setup.py 包含在 distutils 文档中Python 的标准库文档此处。主要区别(对于 python Egg)是您从
setuptools
导入 setup,而不是distutils
。是的。应该是这样。
我不这么认为。
pyc
文件可以依赖于版本和平台。您也许可以打开 Egg(它们应该只是 zip 文件)并删除.py
文件,留下.pyc
文件,但不建议这样做。我不确定。这可能就是“开发模式”。或者您正在寻找某种“py2exe”或“py2app”模式?
You are reading the wrong documentation. You want this: https://setuptools.readthedocs.io/en/latest/setuptools.html#develop-deploy-the-project-source-in-development-mode
Creating setup.py is covered in the distutils documentation in Python's standard library documentation here. The main difference (for python eggs) is you
import setup
fromsetuptools
, notdistutils
.Yep. That should be right.
I don't think so.
pyc
files can be version and platform dependent. You might be able to open the egg (they should just be zip files) and delete.py
files leaving.pyc
files, but it wouldn't be recommended.I'm not sure. That might be “Development Mode”. Or are you looking for some “py2exe” or “py2app” mode?
对于 #4,最接近使用应用程序的 jar 文件启动 java 的是 Python 2.6 中的一个新功能,可执行 zip 文件和目录。
其中 myapp.zip 是包含
__main__.py
文件的 zip,该文件作为要执行的脚本文件执行。您的包依赖项也可以包含在该文件中:您还可以执行一个 Egg,但咒语不太好:
这会将 myapp.egg 放在 Python 路径上并使用 -m 参数来运行模块。您的 myapp.egg 可能类似于:
并且 python 将运行
__init__.py
(您应该在应用程序中检查__file__=='__main__'
以供命令行使用) 。Egg 文件只是 zip 文件,因此您可以使用 zip 工具将
__main__.py
添加到您的 Egg 中,并使其在 python 2.6 中可执行,并像python myapp.egg
一样运行它code> 而不是上面设置 PYTHONPATH 环境变量的咒语。有关可执行 zip 文件的更多信息,包括如何使用 shebang 使其直接可执行,可以在 Michael Foord 关于该主题的博客文章。
For #4, the closest thing to starting java with a jar file for your app is a new feature in Python 2.6, executable zip files and directories.
Where myapp.zip is a zip containing a
__main__.py
file which is executed as the script file to be executed. Your package dependencies can also be included in the file:You can also execute an egg, but the incantation is not as nice:
This puts the myapp.egg on the Python path and uses the -m argument to run a module. Your myapp.egg will likely look something like:
And python will run
__init__.py
(you should check that__file__=='__main__'
in your app for command line use).Egg files are just zip files so you might be able to add
__main__.py
to your egg with a zip tool and make it executable in python 2.6 and run it likepython myapp.egg
instead of the above incantation where the PYTHONPATH environment variable is set.More information on executable zip files including how to make them directly executable with a shebang can be found on Michael Foord's blog post on the subject.
我认为你现在应该使用 pythonwheels 来分发而不是egg。
I think you should use python wheels for distribution instead of egg now.