Search code examples
pythondatabasepicklepython-standalone

Data storage for standalone python application


I want to make a python program (with a PyQt GUI, but I don't know whether that is relevant) that has to save some information that I want to store even when the program closes. Example for information I want to store:

  1. The user can search for a file in a file dialog window. I want to start the file dialog window in the previously used directory, even if the program is closed in between file searches.

  2. The user can enter their own categories to sort items, building up on some of my predefined categories. These new categories should be available the next time the program starts.

Now I'm wondering what the proper way to store such information is. Should I use pickle? A proper database (I know a tiny bit of sqlite3, but would have to read up on that)? A simple text file that I parse myself? One thing for data like in example 1., another for data like in example 2.?

Also, whatever way to store it I use, where would I put that file?

I'm asking in the context that I might want to later make my program available to others as a standalone application (using py2app, py2exe or PyInstaller).

Right now I'm just saving a pickle file in the directory that my .py file is in, like this answer reconmends, but the answer also specifically mentions:

for a personal project it might be enough.

(emphasis mine)

Is using pickle also the "proper, professional" way, if I want to make the program available to other people as a standalone application?


Solution

  • Choice depends on your approach to data you store, which is yours?:

    • user should be able to alter it without usage of my program
    • user should be prevented from altering it with program other than my program

    If first you might consider deploying JSON open-standard file format, for which Python has ready library called json. In effect you get text (which you can save to file) which is human-readable and can be edited in text editor. Also there exist JSON file viewers and editors which made viewing/editing of JSON files easier.