Add data folder recursively using data_files in Python

662 views Asked by At

I want to add folders mydata1 and mydata2 in root package recursively when install my project. So I write this function in setup.py:

def gen_data_files(*dirs):
    results=[]
    for datadir in dirs:
        results.extend([(p, [os.path.join(p,f) for f in files]) for p, subdirs, files in os.walk(datadir)])
    return results

And then in setup.py:

setup(
    #...
    data_files=gen_data_files('mydata2', 'mydata2'),
    #...
)

Creating the sdist is ok: setup.py sdist upload -r mypypi

But when I install, an exception was throw: ValueError: too many values to unpack (expected 2).

I'm new in Python. So I don't know why. Can you help me in this case? Thanks!

EDIT: Those codes have the same results:

results.extend([(p, [os.path.join(p,f) for f in files]) for p, subdirs, files in os.walk(datadir)])

results.extend((p, [os.path.join(p,f) for f in files]) for p, subdirs, files in os.walk(datadir))
2

There are 2 answers

1
Gerges On

The function should be:

def gen_data_files(*dirs):
    results = []
    for datadir in dirs:
        for p, subdirs, files in os.walk(datadir):
            results.extend((p, os.path.join(p, f)) for f in files)
    return results

Also, may I suggest using setuptools.

0
aviit On

Sorry everybody, the issue is my version 5.99999. Too long. Use somethings like 6.0 fixed.