如何在setuptools/distribute中包含包数据?

2024-05-29 01:43:46 发布

您现在位置:Python中文网/ 问答频道 /正文

使用setuptools/distribute时,我无法让安装程序拉入任何^{}文件。我读到的每一篇文章都说以下是正确的方法。有人能建议一下吗?

setup(
   name='myapp',
   packages=find_packages(),
   package_data={
      'myapp': ['data/*.txt'],
   },
   include_package_data=True,
   zip_safe=False,
   install_requires=['distribute'],
)

其中myapp/data/是数据文件的位置。


Tags: 文件方法nametxtpackagedataincludepackages
3条回答

我意识到这是一个老问题,但是对于通过Google找到自己的路的人来说:package_data是一个低谷,dirty lie。它只在构建二进制包时使用(python setup.py bdist ...),而在构建源包时不使用(python setup.py sdist ...)。当然,这是荒谬的——人们会期望构建一个源发行版会产生一个文件集合,这些文件可以发送给其他人来构建二进制发行版。

在任何情况下,对于二进制分布和源分布,使用^{}都可以工作。

我也有同样的问题。解决方法,就是简单地删除include_package_data=True

reading here之后,我意识到include_package_data的目标是包含版本控制中的文件,而不是仅仅“包含包数据”(顾名思义)。从文档中:

The data files [of include_package_data] must be under CVS or Subversion control

...

If you want finer-grained control over what files are included (for example, if you have documentation files in your package directories and want to exclude them from installation), then you can also use the package_data keyword.

去掉这个参数就解决了这个问题,这也是为什么当你切换到distutils时它也能工作的原因,因为它不接受这个参数。

按照@Joe的建议删除include_package_data=True行也对我有效。

更详细地说,我有noMANIFEST.in文件。我用的是Git而不是CVS。

存储库的形状如下:

/myrepo
    - .git/
    - setup.py
    - myproject
        - __init__.py
        - some_mod
            - __init__.py
            - animals.py
            - rocks.py
        - config
            - __init__.py
            - settings.py
            - other_settings.special
            - cool.huh
            - other_settings.xml
        - words
            - __init__.py
            word_set.txt

setup.py

from setuptools import setup, find_packages
import os.path

setup (
    name='myproject',
    version = "4.19",
    packages = find_packages(),  
    # package_dir={'mypkg': 'src/mypkg'},  # didnt use this.
    package_data = {
        # If any package contains *.txt or *.rst files, include them:
        '': ['*.txt', '*.xml', '*.special', '*.huh'],
    },

#
    # Oddly enough, include_package_data=True prevented package_data from working.
    # include_package_data=True, # Commented out.
    data_files=[
#               ('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
        ('/opt/local/myproject/etc', ['myproject/config/settings.py', 'myproject/config/other_settings.special']),
        ('/opt/local/myproject/etc', [os.path.join('myproject/config', 'cool.huh')]),
#
        ('/opt/local/myproject/etc', [os.path.join('myproject/config', 'other_settings.xml')]),
        ('/opt/local/myproject/data', [os.path.join('myproject/words', 'word_set.txt')]),
    ],

    install_requires=[ 'jsonschema',
        'logging', ],

     entry_points = {
        'console_scripts': [
            # Blah...
        ], },
)

我为源发行版运行python setup.py sdist(没有尝试二进制)。

在一个全新的虚拟环境中,我有一个myproject-4.19.tar.gz,文件, 我用

(venv) pip install ~/myproject-4.19.tar.gz
...

除了安装到我的虚拟环境的site-packages中的所有内容之外,这些特殊的数据文件还安装到/opt/local/myproject/data/opt/local/myproject/etc

相关问题 更多 >

    热门问题