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

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


当前回答

在setup.py下的setup(:

setup(
   name = 'foo library'
   ...
  package_data={
   'foolibrary.folderA': ['*'],     # All files from folder A
   'foolibrary.folderB': ['*.txt']  #All text files from folder B
   },

其他回答

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

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

文档可以在这里找到

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

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

我只是想跟进我在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

现在是2019年,以下是行之有效的方法 尽管这里和那里的建议,我在互联网上发现的是使用setuptools_scm,作为选项传递给setuptools.setup。这将包括VCS中版本化的任何数据文件,无论是git还是其他任何文件,到wheel包中,并将从git存储库中进行“pip install”以将这些文件带进来。

因此,我只是在“setup.py”的设置调用中添加了这两行。无需额外安装或导入:

    setup_requires=['setuptools_scm'],
    include_package_data=True,

No need to manually list package_data, or in a MANIFEST.in file - if it is versioned, it is included in the package. The docs on "setuptools_scm" put emphasis on creating a version number from the commit position, and disregard the really important part of adding the data files. (I can't care less if my intermediate wheel file is named "*0.2.2.dev45+g3495a1f" or will use the hardcoded version number "0.3.0dev0" I've typed in - but leaving crucial files for the program to work behind is somewhat important)

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

需要将该文件添加到源压缩文件中 需要修改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'])]

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