将文件包含在 Python 发行版中的 2 种技术:哪种更好?
我正在将一个小型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 betterpackage_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, andpackage_data
for files you use that aren't Python code (like an image or template).