2

I'm looking for a way to add non coding files in the distribution package. For now I succeed to add files within packages but it feels weird to make a python package for files that have nothing to do with python code. (But maybe this statement is the origin of my issue).

What works

This is my structure:

├── data
|   ├── __init__.py
│   └── text
├── myapp
│   ├── __init__.py
│   └── start.py
├── README.md
├── setup.py
└── tests

Here the code of start.py:

from pkg_resources import resource_filename

def main():
    print('Hello world')
    my_data = resource_filename('data', 'text')
    with open(my_data, 'r') as f:
        print(f.read())

And here my setup.py:

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="myapp",
    version="0.0.1",
    packages=setuptools.find_packages(),
    entry_points={
        'console_scripts':[
            'myapp-run = myapp.start:main'
        ]
    },
    include_package_data=True,
    package_data={'':['text']},
    description="A python test project",
    long_description=long_description,
    long_description_content_type="text/markdown",
)

I built this code mixing this answer with this one

If I run the code (in docker container to reset the system easily):

# python setup.py install
# myapp-run
Hello world
Some text

Everything runs has expected.

What needs a "hack" to work

But if I build the distribution the data file is not included:

# python setup.py sdist
...
copying files to myapp-0.0.1...
copying README.md -> myapp-0.0.1
copying setup.py -> myapp-0.0.1
copying data/__init__.py -> myapp-0.0.1/data
copying myapp/__init__.py -> myapp-0.0.1/myapp
copying myapp/start.py -> myapp-0.0.1/myapp
copying myapp.egg-info/PKG-INFO -> myapp-0.0.1/myapp.egg-info
copying myapp.egg-info/SOURCES.txt -> myapp-0.0.1/myapp.egg-info
copying myapp.egg-info/dependency_links.txt -> myapp-0.0.1/myapp.egg-info
copying myapp.egg-info/entry_points.txt -> myapp-0.0.1/myapp.egg-info
copying myapp.egg-info/top_level.txt -> myapp-0.0.1/myapp.egg-info
Writing myapp-0.0.1/setup.cfg
Creating tar archive

This can be fix by removing the include_package_data=True from setup.py as stated here but nobody seems to know why. I'm also surprised that python setup bdist_wheel works without having to touch at this option.

What doesn't work at all

Now from there, if I remove the data/__init__.py file, turning the data in simple folder, I obviously get a module not found error as package_data and resource_filename are designed for packages.

I tried to replace the package_data instruction by data_files = [('myapp-data',['data/text'])] following this documentation for distutils (I understood setuptools and distutils have been merge at one point).

This includes my file (copying data/text -> build/bdist.linux-x86_64/egg/myapp-data) but I don't know how to access it from my code.

All this code is executed in a docker python:3.6-strech container

pjs
  • 18,696
  • 4
  • 27
  • 56
Plup
  • 750
  • 1
  • 6
  • 13
  • `include_package_data=True` seems to be related to the `MANIFEST.in`. As I don't have one it might not work properly. – Plup Oct 30 '18 at 21:26

0 Answers0