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

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


当前回答

这里有一个对我有用的更简单的答案。

首先,根据上面Python Dev的注释,setuptools是不需要的:

package_data is also available to pure distutils setup scripts 
since 2.3. – Éric Araujo

这很好,因为在包中添加setuptools要求意味着您也必须安装它。简而言之:

from distutils.core import setup

setup(
    # ...snip...
    packages          = ['pkgname'],
    package_data      = {'pkgname': ['license.txt']},
)

其他回答

对于要包含在安装中的非python文件,它们必须位于已安装包目录中的一个目录中。如果您在MANIFEST中指定包目录之外的非python文件。中,它们将包含在您的发行版中,但不会被安装。在包目录之外安装任意文件的“文档化”方式并不可靠(现在每个人都注意到了)。

The above answer from Julian Mann copies the files to your package directory in the build directory, so it does work, but not if you are installing in editable/develop mode (pip install -e or python setup.py develop). Based on this answer to a related question (and Julian's answer), below is an example that copies files to your installed package location either way after all the other install/develop tasks are done. The assumption here is that files file1 and file2 in your root-level data directory will be copied to your installed package directory (my_package), and that they will be accessible from python modules in your package using os.path.join(os.path.dirname(__file__), 'file1'), etc.

记得也要做清单。在上面描述的东西中,以便这些文件也包含在您的发行版中。为什么setuptools会在你的发行版中包含文件,然后默默地不安装它们,这超出了我的理解范围。尽管在包目录之外安装它们可能更可疑。

import os
from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install
from shutil import copyfile

HERE = os.path.abspath(os.path.dirname(__file__))
NAME = 'my_package'

def copy_files (target_path):
    source_path = os.path.join(HERE, 'data')
    for fn in ["file1", "file2"]:
        copyfile(os.path.join(source_path, fn), os.path.join(target_path,fn))

class PostDevelopCommand(develop):
    """Post-installation for development mode."""
    def run(self):
        develop.run(self)
        copy_files (os.path.abspath(NAME))

class PostInstallCommand(install):
    """Post-installation for installation mode."""
    def run(self):
        install.run(self)
        copy_files (os.path.abspath(os.path.join(self.install_lib, NAME)))

setup(
    name=NAME,
    cmdclass={
        'develop': PostDevelopCommand,
        'install': PostInstallCommand,
    },
    version='0.1.0',
    packages=[NAME],
    include_package_data=True,
    setup_requires=['setuptools_scm'],
)

这里有一个对我有用的更简单的答案。

首先,根据上面Python Dev的注释,setuptools是不需要的:

package_data is also available to pure distutils setup scripts 
since 2.3. – Éric Araujo

这很好,因为在包中添加setuptools要求意味着您也必须安装它。简而言之:

from distutils.core import setup

setup(
    # ...snip...
    packages          = ['pkgname'],
    package_data      = {'pkgname': ['license.txt']},
)

要实现你所描述的需要两步……

需要将该文件添加到源压缩文件中 需要修改Setup.py,将数据文件安装到源路径

步骤1:要将文件添加到源tarball,请将其包含在MANIFEST中

在包含setup.py的文件夹中创建MANIFEST模板

MANIFEST基本上是一个文本文件,其中包含将包含在源tarball中的所有文件的列表。

下面是我项目的MANIFEST:

CHANGELOG.txt INSTALL.txt LICENSE.txt pypreprocessor.py 固定 setup . py test.py TODO.txt

注意:虽然sdist确实会自动添加一些文件,但我更喜欢显式地指定它们,而不是预测它能做什么,不能做什么。

步骤2:要将数据文件安装到源文件夹,请修改setup.py

由于您希望向源安装文件夹添加一个数据文件(LICENSE.txt),因此需要修改数据安装路径以匹配源安装路径。这是必要的,因为默认情况下,数据文件安装到与源文件不同的位置。

修改数据安装目录以匹配源安装目录…

从distutils中获取安装目录信息:

from distutils.command.install import INSTALL_SCHEMES

修改数据安装目录以匹配源安装目录:

for scheme in INSTALL_SCHEMES.values():
    scheme['data'] = scheme['purelib']

然后,将数据文件和位置添加到setup():

data_files=[('', ['LICENSE.txt'])]

注意:上面的步骤应该以标准的方式完成您所描述的工作,而不需要任何扩展库。

创建清单。在项目根用递归include到所需目录或用文件名include。

include LICENSE
include README.rst
recursive-include package/static *
recursive-include package/templates *

文档可以在这里找到

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

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