简体   繁体   English

打包python编写的代码时,如何在代码中添加Google drive api的json凭证文件?

[英]How to add a json credential file of Google drive api alongwith the code when packaging the code written in python?

I wrote a function in python which uses the json file as the credential file for the Google drive API.我在 python 中编写了一个函数,它使用 json 文件作为 Google Drive API 的凭证文件。 The function takes google sheet URL as input and plots graph with the help of gspread library.该函数以 google sheet URL 作为输入,并在 gspread 库的帮助下绘制图形。 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?如果我必须制作该函数的包,我应该如何包含具有相同路径的 json 文件以使该函数作为包工作? I tried using data_files and MANIFEST.in file as well, but of no use.我也尝试使用 data_files 和 MANIFEST.in 文件,但没有用。 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.我正在使用 setuptools 来打包代码。 Any help is appreciated.任何帮助表示赞赏。

setuptools has three different ways to specify data files that should be packages together with your project, Manifest.in , data_files , and package_data . setuptools提供了三种不同的方法来指定应该与您的项目一起打包的数据文件,即Manifest.indata_filespackage_data For config files that are under your source tree, package_data is usually the easiest to use.对于源代码树下的配置文件, package_data通常是最容易使用的。


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 :您需要一个至少具有此配置的setup.py文件来打包tmp下的所有代码和.json文件:

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"} . keys.json文件只包含{"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):从包中解析打包配置文件的最安全方法是使用importlib.resources来访问它,特别是它的path函数(如果你坚持使用 python 3.7 或更早版本, importlib_resources是等效的 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 .一旦我使用pip install -e .安装了这个包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 .如果您打算使用 python 打包一段时间,请考虑使用比 setuptools 更现代的构建后端,例如诗歌flit They make the experience a lot more bearable.他们使经验很多更惬意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM