在setup.py中包含非Python文件 [英] Including non-Python files with setup.py

查看:273
本文介绍了在setup.py中包含非Python文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使 setup.py 包含不属于代码的文件? (具体来说,这是一个许可证文件,但是也可以是其他任何东西。)



我希望能够控制文件的位置。在原始源文件夹中,该文件位于包的根目录中。 (即与最高的 __ init __。py 处于同一级别。)我希望它在安装软件包时完全保持在该位置,而不管操作系统是什么。我该怎么做?

解决方案

可能最好的方法是使用 setuptools package_data 指令。这确实意味着使用 setuptools (或< a href = http://pypi.python.org/pypi/distribute/ rel = noreferrer> 分发 ),而不是 distutils ,但这是一个非常无缝的升级。



这里有一个完整(但未经测试)的示例:

 从setuptools导入安装程序,find_packages 

安装程序(
name ='your_project_name',
版本='0.1',
description ='A description。',
packages = find_packages(排除= ['ez_setup','tests','tests。*']),
package_data = {'':['license.txt']},
include_package_data = True,
install_requires = [],

请注意此处的关键行:

  package_data = { :['license.txt']},
include_package_data = True,

package_data dict 的包名称(空=所有包)到模式列表(可以包括glob)。例如,如果只想指定包中的文件,也可以这样做:

  package_data = {'yourpackage' :['* .txt','path / to / resources / *。txt']} 

这里的解决方案肯定是 not ,用 .py 重命名非 py 文件扩展。



请参见 Ian Bicking的演示了解更多信息。



更新:另一种[更好的方法]



如果您只想控制源代码分发的内容( sdist )并在包外部(例如顶级目录)添加文件,即可添加 MANIFEST.in 文件。有关此文件的格式,请参见 Python文档



自从编写此回复以来,我发现使用 MANIFEST.in 通常不会让人感到沮丧。确保您的源分发版( tar.gz )具有所需的文件。



例如,如果您要从顶级包含 requirements.txt ,则递归包括顶级-级别数据目录:

  include requirements.txt 
递归包括数据*

尽管如此,为了在安装时将这些文件复制到site-packages内的软件包文件夹中,您需要提供 include_package_data = True setup()函数。有关更多信息,请参见添加非代码文件。 / p>

How do I make setup.py include a file that isn't part of the code? (Specifically, it's a license file, but it could be any other thing.)

I want to be able to control the location of the file. In the original source folder, the file is in the root of the package. (i.e. on the same level as the topmost __init__.py.) I want it to stay exactly there when the package is installed, regardless of operating system. How do I do that?

解决方案

Probably the best way to do this is to use the setuptools package_data directive. This does mean using setuptools (or distribute) instead of distutils, but this is a very seamless "upgrade".

Here's a full (but untested) example:

from setuptools import setup, find_packages

setup(
    name='your_project_name',
    version='0.1',
    description='A description.',
    packages=find_packages(exclude=['ez_setup', 'tests', 'tests.*']),
    package_data={'': ['license.txt']},
    include_package_data=True,
    install_requires=[],
)

Note the specific lines that are critical here:

package_data={'': ['license.txt']},
include_package_data=True,

package_data is a dict of package names (empty = all packages) to a list of patterns (can include globs). For example, if you want to only specify files within your package, you can do that too:

package_data={'yourpackage': ['*.txt', 'path/to/resources/*.txt']}

The solution here is definitely not to rename your non-py files with a .py extension.

See Ian Bicking's presentation for more info.

UPDATE: Another [Better] Approach

Another approach that works well if you just want to control the contents of the source distribution (sdist) and have files outside of the package (e.g. top-level directory) is to add a MANIFEST.in file. See the Python documentation for the format of this file.

Since writing this response, I have found that using MANIFEST.in is typically a less frustrating approach to just make sure your source distribution (tar.gz) has the files you need.

For example, if you wanted to include the requirements.txt from top-level, recursively include the top-level "data" directory:

include requirements.txt
recursive-include data *

Nevertheless, in order for these files to be copied at install time to the package’s folder inside site-packages, you’ll need to supply include_package_data=True to the setup() function. See Adding Non-Code Files for more information.

这篇关于在setup.py中包含非Python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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