9

Today I created a setup.py-file and found one working script and two none working scenarios. In the two non working scenarios the package_data after is missing in the build. I created the build with python setup.py sdist.

Why scenario 2 and 3 don't include my package data?

Scenario 1 works:

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'],
    }
)

Scenario 2, doesn't work:

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'],
    }
)

Scenario 3 doesn't work either:

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 Answers 2

5

In my case, the problem wasn't in setup.py but the missing MANIFEST.in, which needs to also declare package data.

Sign up to request clarification or add additional context in comments.

3 Comments

thats not true, if package_data is declared correctly it goes into Manifest automatically.
@marscher, package data doesnot work in source distributions.. you need MANIFEST.in .. check stackoverflow.com/a/14159430/1327005 so tsg is right
Yes you would need it both in setup.py package_data and MANIFEST.in
3

you can not use glob syntax directly in package_data declaration.

but you can declare a variable containing this data before passing it to setup function:

from glob import glob

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

...

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

2 Comments

Huh. After all my work mucking around with MANIFESTS, this appears to actually be the correct answer (at least in setuptools).
This is not correct, setuptools uses glob by default: package_data = {'': ['*.dat']}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.