如何创建Python Egg文件

发布于 2024-08-17 01:45:26 字数 467 浏览 13 评论 0原文

我对 Python 中的 Egg 文件有疑问。

我有很多按包组织的Python代码,我正在尝试创建egg文件。 我遵循说明,但它们很常见。

据此,看来我需要有一个 setup.py 文件。

  1. 您能告诉我需要在 setup.py 文件中放入什么内容以及它应该驻留在哪里吗?
  2. 我想创建 setup.py 然后启动“setup.py bdist_egg”来获取 Egg 文件就足够了。您能确认一下吗?
  3. 是否可以仅将 .pyc 文件包含到 Egg 文件中?
  4. 拥有 .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.

  1. Would you please tell me what I need to put into setup.py file and where it should reside?
  2. I suppose it's enough to create setup.py and then start "setup.py bdist_egg" for getting egg file. Could you please confirm?
  3. Is it possible to include only .pyc files into egg file?
  4. Having .egg file how I can just start the code from it without unpacking like java -jar <jar file> does?

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

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

发布评论

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

评论(3

寂寞笑我太脆弱 2024-08-24 01:45:26

您正在阅读错误的文档。您想要这个: https ://setuptools.readthedocs.io/en/latest/setuptools.html#develop-deploy-the-project-source-in-development-mode

  1. 创建 setup.py 包含在 distutils 文档中Python 的标准库文档此处。主要区别(对于 python Egg)是您从 setuptools 导入 setup,而不是 distutils

  2. 是的。应该是这样。

  3. 我不这么认为。 pyc 文件可以依赖于版本和平台。您也许可以打开 Egg(它们应该只是 zip 文件)并删除 .py 文件,留下 .pyc 文件,但不建议这样做。

  4. 我不确定。这可能就是“开发模式”。或者您正在寻找某种“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

  1. 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 from setuptools, not distutils.

  2. Yep. That should be right.

  3. 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.

  4. I'm not sure. That might be “Development Mode”. Or are you looking for some “py2exe” or “py2app” mode?

世俗缘 2024-08-24 01:45:26

对于 #4,最接近使用应用程序的 jar 文件启动 java 的是 Python 2.6 中的一个新功能,可执行 zip 文件和目录

python myapp.zip

其中 myapp.zip 是包含 __main__.py 文件的 zip,该文件作为要执行的脚本文件执行。您的包依赖项也可以包含在该文件中:

__main__.py
mypackage/__init__.py
mypackage/someliblibfile.py

您还可以执行一个 Egg,但咒语不太好:

# Bourn Shell and derivatives (Linux/OSX/Unix)
PYTHONPATH=myapp.egg python -m myapp
rem Windows 
set PYTHONPATH=myapp.egg
python -m myapp

这会将 myapp.egg 放在 Python 路径上并使用 -m 参数来运行模块。您的 myapp.egg 可能类似于:

myapp/__init__.py
myapp/somelibfile.py

并且 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.

python myapp.zip

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:

__main__.py
mypackage/__init__.py
mypackage/someliblibfile.py

You can also execute an egg, but the incantation is not as nice:

# Bourn Shell and derivatives (Linux/OSX/Unix)
PYTHONPATH=myapp.egg python -m myapp
rem Windows 
set PYTHONPATH=myapp.egg
python -m myapp

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:

myapp/__init__.py
myapp/somelibfile.py

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 like python 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.

jJeQQOZ5 2024-08-24 01:45:26

我认为你现在应该使用 pythonwheels 来分发而不是egg。

Wheels 是 python 发行版的新标准,旨在
更换鸡蛋。 pip >= 1.4 和 setuptools >= 0.8 提供支持。

I think you should use python wheels for distribution instead of egg now.

Wheels are the new standard of python distribution and are intended to
replace eggs. Support is offered in pip >= 1.4 and setuptools >= 0.8.

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