在Python发行版中包含文件的2种技术:哪个更好? [英] 2 techniques for including files in a Python distribution: which is better?

查看:78
本文介绍了在Python发行版中包含文件的2种技术:哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

方法1:

将此代码包含在setup.py中:

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'])]
      )

方法2:

包括此setup.py中的代码:

Include this code in setup.py:

from distutils.core import setup

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

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

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

include config\* 

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

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?

推荐答案

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

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

这篇关于在Python发行版中包含文件的2种技术:哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆