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

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


当前回答

我想对其中一个问题发表评论,但我没有足够的声誉来做>.>

以下是对我有效的方法(参考文档后想到的):

package_data={
    'mypkg': ['../*.txt']
},

include_package_data: False

奇怪的是,最后一行对我来说也很重要(你也可以省略这个关键字参数——它的工作原理是一样的)。

它的作用是复制顶级目录或根目录中的所有文本文件(比您想分发的包mypkg高一级)。

其他回答

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

在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 *

文档可以在这里找到

这在2020年有效!

正如其他人所说,创造“显化”。在“setup.py所在的位置”。

接下来在manifest中包含/排除所有必要的东西。这里要注意语法。 例如:假设我们有模板文件夹要包含在源包中。

在manifest文件中这样做:

recursive-include template *

确保像上面那样在文件/dirs的dir-name和pattern之间留有空格。 不要像我们在gitignore那样做

recursive-include template/* [this won't work]

其他选项是使用include。有很多选择。看看他们的Manifest.in文件

最后重要的一步是,在setup.py中包含这个参数,然后就可以开始了!

   setup(
    ...
    include_package_data=True,
    ......
)

希望有帮助!编码快乐!

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

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