2

I wrote a function in python which uses the json file as the credential file for the Google drive API. The function takes google sheet URL as input and plots graph with the help of gspread library. If I have to make a package of that function how should I include the json file with same path to make the function work as a package? I tried using data_files and MANIFEST.in file as well, but of no use. Upon building the package, I still get the error:

No file named credentials.json found

when I am testing the package with:

pip install -e .

I am using setuptools for packaging the code. Any help is appreciated.

Utkarsh
  • 43
  • 5

1 Answers1

0

setuptools has three different ways to specify data files that should be packages together with your project, Manifest.in, data_files, and package_data. For config files that are under your source tree, package_data is usually the easiest to use.


Given a very small sample project that looks like this:

.
├───setup.py
└───tmp
    ├───__init__.py 
    └───keys.json

You'd need a setup.py file with at least this configuration to package all code and .json files under tmp :

from setuptools import setup

setup(
    name="tmp",
    version="0.1.0",
    packages=["tmp"],
    package_data={"tmp": ["*.json"]},
)

The keys.json file only contains {"key": "foo"}.

The safest way to parse packaged config files from within your package is to use importlib.resources to access it, in particular its path function (if you are stuck on python 3.7 or older, importlib_resources is an equivalent pip-installable backport):

tmp/__init__.py

from importlib import resources
import json

def print_keys():
    with resources.path("tmp", "keys.json") as foo_path:
        print(json.load(foo_path.open()))

Once I install this package with pip install -e ., I can run:

>>> import tmp
>>> tmp.print_keys()
{'key': 'foo'}

If you plan to work with python packaging for a while, consider using a more modern build-backend than setuptools, such as poetry or flit. They make the experience a lot more bearable.

Arne
  • 17,706
  • 5
  • 83
  • 99
  • I tried doing it, but it is not working in my case at least. It still shows No .json file found. – Utkarsh Nov 27 '20 at 07:59
  • are you sure the path to the json file is correct? If it doesn't even work during a local editable install, then the error can't happen during packaging - so it should be during file access – Arne Nov 27 '20 at 13:17