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

我希望能够控制文件的位置。在原始源文件夹中,文件位于包的根目录中。(即与最顶层的__init__.py在同一层。)我希望它在安装包时保持在那里,而不管操作系统是什么。我怎么做呢?


当前回答

以上这些方法对我都不起作用。是这个回答救了我。 显然,为了在安装期间提取这些数据文件,我必须做几件事:

Like already mentioned - Add a MANIFEST.in to the project and specify the folder/files you want to be included. In my case: recursive-include folder_with_extra_stuff * Again, like already mentioned - Add include_package_data=True to your setup.py. This is crucial, because without it only the files that match *.py will be brought. This is what was missing! - Add an empty __init__.py to your data folder. For me I had to add this file to my folder-with-extra-stuff. Extra - Not sure if this is a requirement, but with my own python modules I saw that they're zipped inside the .egg file in site-packages. So I had to add zip_safe=False to my setup.py file.

最终目录结构

my-app/
├─ app/
│  ├─ __init__.py
│  ├─ __main__.py
├─ folder-with-extra-stuff/
│  ├─ __init__.py
│  ├─ data_file.json
├─ setup.py
├─ MANIFEST.in

其他回答

我找到了一个解决办法:我将我的lgpl2.1 .1_license.txt重命名为lgpl2.1 .1_license.txt.py,并在文本周围加上一些三引号。现在我不需要使用data_files选项,也不需要指定任何绝对路径。我知道把它变成Python模块很难看,但我认为它没有指定绝对路径难看。

以上这些方法对我都不起作用。是这个回答救了我。 显然,为了在安装期间提取这些数据文件,我必须做几件事:

Like already mentioned - Add a MANIFEST.in to the project and specify the folder/files you want to be included. In my case: recursive-include folder_with_extra_stuff * Again, like already mentioned - Add include_package_data=True to your setup.py. This is crucial, because without it only the files that match *.py will be brought. This is what was missing! - Add an empty __init__.py to your data folder. For me I had to add this file to my folder-with-extra-stuff. Extra - Not sure if this is a requirement, but with my own python modules I saw that they're zipped inside the .egg file in site-packages. So I had to add zip_safe=False to my setup.py file.

最终目录结构

my-app/
├─ app/
│  ├─ __init__.py
│  ├─ __main__.py
├─ folder-with-extra-stuff/
│  ├─ __init__.py
│  ├─ data_file.json
├─ setup.py
├─ MANIFEST.in

我只是想跟进我在Centos 6上的Python 2.7中发现的一些东西。如上所述,添加package_data或data_files对我不起作用。我加了一份清单。IN中有我想要的文件,这些文件将非python文件放入tarball中,但没有通过RPM将它们安装到目标机器上。

最后,我能够使用setup/setuptools中的“选项”将文件导入到我的解决方案中。选项文件允许您从setup.py修改规范文件的各个部分。如下。

from setuptools import setup


setup(
    name='theProjectName',
    version='1',
    packages=['thePackage'],
    url='',
    license='',
    author='me',
    author_email='me@email.com',
    description='',
    options={'bdist_rpm': {'install_script': 'filewithinstallcommands'}},
)

file - MANIFEST.in:

include license.txt

文件 - 文件与安装命令:

mkdir -p $RPM_BUILD_ROOT/pathtoinstall/
#this line installs your python files
python setup.py install -O1 --root=$RPM_BUILD_ROOT --record=INSTALLED_FILES
#install license.txt into /pathtoinstall folder
install -m 700 license.txt $RPM_BUILD_ROOT/pathtoinstall/
echo /pathtoinstall/license.txt >> INSTALLED_FILES

没有一个答案对我有用,因为我的文件在顶层,在包之外。我使用了自定义构建命令。

import os
import setuptools
from setuptools.command.build_py import build_py
from shutil import copyfile

HERE = os.path.abspath(os.path.dirname(__file__))
NAME = "thepackage"

class BuildCommand(build_py):
    def run(self):
        build_py.run(self)

        if not self.dry_run:
            target_dir = os.path.join(self.build_lib, NAME)
            for fn in ["VERSION", "LICENSE.txt"]:
                copyfile(os.path.join(HERE, fn), os.path.join(target_dir,fn))

 
 
setuptools.setup(
    name=NAME,
    cmdclass={"build_py": BuildCommand},
    description=DESCRIPTION,
    ...
)

最好的方法可能是使用setuptools package_data指令。这确实意味着使用setuptools(或distribute)而不是distutils,但这是一个非常无缝的“升级”。

下面是一个完整的(但未经测试的)例子:

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=[],
)

注意这里的关键行:

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

Package_data是包名(空=所有包)到模式列表(可以包括glob)的字典。例如,如果你只想在你的包中指定文件,你也可以这样做:

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

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

更多信息请看Ian Bicking的演讲。

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

如果你只是想控制源分发(sdist)的内容,并且在包之外有文件(例如顶级目录),另一种工作得很好的方法是添加一个MANIFEST。在文件中。有关此文件的格式,请参阅Python文档。

写完这篇文章后,我发现使用MANIFEST。In通常是一种不那么令人沮丧的方法,可以确保您的源发行版(tar.gz)有您需要的文件。

例如,如果你想包含顶层的requirements.txt,递归地包含顶层的"data"目录:

include requirements.txt
recursive-include data *

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