我如何使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']},
)

其他回答

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

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

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

include_package_data: False

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

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

步骤1:创建MANIFEST。和setup.py在同一个文件夹里

步骤2:在MANIFEST.in中包含要添加的文件的相对路径

include README.rst
include docs/*.txt
include funniest/data.json

步骤3:在setup()函数中设置include_package_data=True,将这些文件复制到site-package

参考资料在这里。

现在是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)

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

首先,根据上面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下的setup(:

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