8

今天我创建了一个 setup.py 文件,发现一个工作脚本和两个无工作场景。在两个非工作场景package_data中,构建中缺少 after。我用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'],
    }
)
4

2 回答 2

5

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

于 2013-02-09T20:21:19.413 回答
3

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

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

from glob import glob

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

...

setup(...
package_data = {
    '': txt_files,
    'mypkg': data,
}
...
于 2014-10-14T15:29:54.667 回答