将文件包含在 Python 发行版中的 2 种技术:哪种更好?

发布于 2024-09-04 03:00:29 字数 882 浏览 2 评论 0原文

我正在将一个小型 Python 项目打包为 zip 或 Egg 文件,以便可以分发。我遇到了两种包含项目配置文件的方法,这两种方法似乎都会产生相同的结果。

方法 1:

在 setup.py 中包含此代码:

from distutils.core import setup

setup(name='ProjectName', 
      version='1.0', 
      packages=['somePackage'],
      data_files = [('config', ['config\propFiles1.ini', 
                                'config\propFiles2.ini', 
                                'config\propFiles3.ini'])]
      )

方法 2:

在 setup.py 中包含此代码:

from distutils.core import setup

setup(name='ProjectName', 
      version='1.0', 
      packages=['somePackage']
      )

然后,创建一个 MANIFEST.in 文件,其中包含以下行:

include config\* 

方法之间有什么区别吗?哪一个是首选?我倾向于第一种,因为这样根本不需要 MANIFEST.in 文件。但是,在第一种方法中,您必须单独指定每个文件,而在第二种方法中,您可以只包含整个文件夹。还有什么我应该考虑的吗?标准做法是什么?

I'm working on packaging a small Python project as a zip or egg file so that it can be distributed. I've come across 2 ways to include the project's config files, both of which seem to produce identical results.

Method 1:

Include this code in setup.py:

from distutils.core import setup

setup(name='ProjectName', 
      version='1.0', 
      packages=['somePackage'],
      data_files = [('config', ['config\propFiles1.ini', 
                                'config\propFiles2.ini', 
                                'config\propFiles3.ini'])]
      )

Method 2:

Include this code in setup.py:

from distutils.core import setup

setup(name='ProjectName', 
      version='1.0', 
      packages=['somePackage']
      )

Then, create a MANIFEST.in file with this line in it:

include config\* 

Is there any difference between the methods? Which one is preferred? I tend to lean towards the first because then no MANIFEST.in file is necessary at all. However, in the first method you have to specify each file individually while in the second you can just include the whole folder. Is there anything else I should be taking into consideration? What's the standard practice?

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

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

发布评论

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

评论(1

缘字诀 2024-09-11 03:00:29

MANIFEST.in 控制当您调用 python setup.py sdist 时将哪些文件放入分发 zip 文件中。它控制安装的内容。 data_files (或更好的 package_data)控制安装哪些文件(我认为还可以确保文件包含在 zip 文件中)。对于您不会安装的文件(例如文档),使用 MANIFEST.in;对于您使用的非 Python 代码文件(例如图像或模板),使用 package_data

MANIFEST.in controls what files are put into the distribution zip file when you call python setup.py sdist. It does not control what is installed. data_files (or better package_data) controls what files are installed (and I think also makes sure files are included in the zip file). Use MANIFEST.in for files you won't install, like documentation, and package_data for files you use that aren't Python code (like an image or template).

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