Python:为什么setuptools不包含我的包数据?

时间:2012-11-14 14:32:49

标签: python setuptools distutils

今天我创建了一个setup.py文件,找到了一个工作脚本和两个无法工作的场景。 在两个非工作方案中,构建中缺少package_data之后。我使用 python setup.py sdist 创建了构建。

为什么方案2和3不包含我的包数据?

场景1有效:

import os 
from distutils.core import setup

def find_packages(path):
    package_path = os.path.join(os.path.dirname(__file__), path)
    packages = []
    for f in os.walk(package_path):

    directory_name = f[0]

    if directory_name.endswith('egg-info') or directory_name.endswith('path'):
        continue

    directory_name = directory_name.replace(package_path, '').replace('/', '.').strip()

    if not len(directory_name):
        continue

    if directory_name.startswith('.'):
        directory_name = directory_name.replace('.', '', 1)

    packages.append(directory_name)

return packages

setup (
    name = 'mypkg',
    packages = find_packages('src'),
    package_dir = {'mypkg': 'src/mypkg'},
    include_package_data = True,
    package_data = {
        '': ['*.txt'],
        'mypkg': ['data/*.dat'],
    }
)

场景2不起作用:

from setuptools import setup #, find_packages
from setuptools.command import sdist
setup (
    name = 'mypkg',
    packages = ['mypkg'],
    package_dir = {'mypkg': 'src/mypkg'},
    include_package_data = True,
    package_data = {
        '': ['*.txt'],
        'mypkg': ['data/*.dat'],
    }
)

场景3也不起作用:

from setuptools import find_packages
from setuptools.command import sdist
setup (
    name = 'mypkg',
    packages = find_packages('src'),
    package_dir = {'mypkg': 'src/mypkg'},
    include_package_data = True,
    package_data = {
        '': ['*.txt'],
        'mypkg': ['data/*.dat'],
    }
)

2 个答案:

答案 0 :(得分:4)

就我而言,问题不在于setup.py,而是缺少的MANIFEST.in,它还需要声明包数据。

答案 1 :(得分:2)

你不能直接在package_data声明中使用glob语法。

但您可以在将其传递给设置函数之前声明包含此数据的变量:

from glob import glob

data = glob('data/*.dat')
txt_files = glob('*.txt')

...

setup(...
package_data = {
    '': txt_files,
    'mypkg': data,
}
...