20

I want to package .json files as well in the python egg file.

For example: boto package has endpoints.json file. But when I run python setup.py bdist_egg it does not include the json file in the egg. How do I include the Json file in the egg?

How do I include *.json file in the egg?

Below is the setup.py code

from setuptools import setup, find_packages, Extension

setup(
  name='X-py-backend',
  version='tip',
  description='X Python backend tools',
  author='meme',
  packages=find_packages('python'),
  package_dir={'': 'python'},
  data_files=[('boto', ['python/boto/endpoints.json'])],
  namespace_packages = ['br'],
  zip_safe=True,
)

setup(
  name='X-py-backend',
  version='tip',
  packages=find_packages('protobuf/target/python'),
  package_dir={'': 'protobuf/target/python'},
  namespace_packages = ['br'],
  zip_safe=True,
)

3 Answers 3

20

You only need to list the file on data_files parameter. Here is the example.

setup(
  name='X-py-backend',
  version='tip',
  description='XXX Python backend tools',
  author='meme',
  packages=find_packages('python'),
  package_dir={'': 'python'},
  data_files=[('boto', ['boto/*.json'])]
  namespace_packages = ['br'],
  zip_safe=True
)

You can see the details here. https://docs.python.org/2/distutils/setupscript.html#installing-additional-files

Another way to do this is to use MANIFEST.in files. you need to create a MANIFEST.in file in your project root. Here is the example.

include python/boto/endpoints.json

Please visit here for more information.https://docs.python.org/2/distutils/sourcedist.html#manifest-template

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

2 Comments

This does not work. I ran "python setup.py bdist_egg" after making this change. When I unzip the .egg file I don't find "endpoints.json" file inside boto package. Am I missing something here?
Please share your edited setup.py. I will look into it.
13

Well this works for me.

setup.py:

from setuptools import setup, find_packages

setup(
    name="clean",
    version="0.1",
    description="Clean package",
    packages=find_packages() + ['config'],
    include_package_data=True
)

MANIFEST.in:

recursive-include config *

where there is a config file under the project root directory which contains a whole bunch of json files.

Hope this helps.

2 Comments

I had data_files and everything properly included with glob etc, but *.json files were not. After I saw your post with include_package_data kwarg I used it (though I thought it'd work only for package_data) and tada, everything packaged correctly without MANIFEST.in! Thanks!
I did this, but I had to include the MANIFEST.in file unlike @PeterBadida.
0

I was having *json data in resources folder as resources/params.json inside package src and the setup.py configuration that works in my case is as below

from setuptools import setup, find_packages


setup(
    name='project_dummy',
    version='0.1.0',
    packages=find_packages(include=['src', 'src.*']),
    package_data={'src': ['resources/*.json']},
    install_requires=[
        'numpy==1.24',
    ...

Below is the code structure I have

├── setup.py
├── src
│   ├── __init__.py
│   ├── __pycache__
│   ├── config
│   ├── crg
│   ├── resources
│   └── utilities

Comments

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.