Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have persistent storage for a PYPI package

I have a pypi package called collectiondbf which connects to an API with a user entered API key. It is used in a directory to download files like so:

python -m collectiondbf [myargumentshere..]

I know this should be basic knowledge, but I'm really stuck on the question:

How can I save the keys users give me in a meaningful way so that they do not have to enter them every time?

I would like to use the following solution using a config.json file, but how would I know the location of this file if my package will be moving directories?

Here is how I would like to user it but obviously it won't work since the working directory will change

import json
if user_inputed_keys:
    with open('config.json', 'w') as f:
        json.dump({'api_key': api_key}, f)
like image 973
Jesse Reza Khorasanee Avatar asked Feb 26 '20 03:02

Jesse Reza Khorasanee


People also ask

Are PyPI packages free?

Currently, PyPi is free. Hundreds of thousands of Python developers use the repository to find and download packages. Python packages are similar to libraries. Some packages are complimentary, other packages have a price, but the repository is accessible to everyone.

How many PyPI packages are there?

As of 17 January 2022, more than 350,000 Python packages can be accessed through PyPI.

Are PyPI packages open-source?

PyPI makes it easy to distribute and access useful projects that are not a part of the standard Python libraries. It's very simple to publish your own open-source project to PyPI.


1 Answers

Most common operating systems have the concept of an application directory that belongs to every user who has an account on the system. This directory allows said user to create and read, for example, config files and settings.

So, all you need to do is make a list of all distros that you want to support, find out where they like to put user application files, and have a big old if..elif..else chain to open the appropriate directory.


Or use appdirs, which does exactly that already:

from pathlib import Path
import json
import appdirs

CONFIG_DIR = Path(appdirs.user_config_dir(appname='collectiondbf'))  # magic
CONFIG_DIR.mkdir(parents=True, exist_ok=True)

config = CONFIG_DIR / 'config.json'
if not config.exists():
  with config.open('w') as f:
    json.dumps(get_key_from_user(), f)
with config.open('r') as f:
  keys = json.load(f)  # now 'keys' can safely be imported from this module
like image 89
Arne Avatar answered Sep 20 '22 01:09

Arne