[GNC] Python script can't read Gnucash files
Jim DeLaHunt
list+gnucash at jdlh.com
Sat Nov 30 16:24:33 EST 2024
John:
On 2024-11-30 10:55, John Haiducek wrote:
> Hi,
>
> A Python script I've been using with Gnucash recently stopped working, and
> is no longer able to read files that could be read previously with the same
> script. The script can open Gnucash XML files, but it doesn't find the
> accounts in them. The script uses the official Gnucash Python API. Both
> Gnucash and the Python bindings were installed using the Fedora package
> manager.
>
> Here's the script:
>
> ```
> from sys import argv
> from gnucash import Session, SessionOpenMode
>
> def account_from_path(top_account, account_path, original_path=None):
> if original_path==None: original_path = account_path
> account, account_path = account_path[0], account_path[1:]
>
> account = top_account.lookup_by_name(account)
> if account == None:
> raise Exception(
> "path " + ':'.join(original_path) + " could not be found")
>
> if len(account_path) > 0 :
> return account_from_path(account, account_path, original_path)
> else:
> return account
>
> if __name__=='__main__':
>
> gnucash_file = argv[1]
>
> gnucash_session=Session(gnucash_file, SessionOpenMode.SESSION_READ_ONLY)
>
> root_account = gnucash_session.book.get_root_account()
>
> investments = account_from_path(root_account, ['Assets','Investments'])
> ```
>
> I'm experiencing this with Gnucash 5.9 on Fedora 39, installed using the
> Fedora repositories. I'm not sure how to tell whether this is a Gnucash
> bug, or a packaging error on the Fedora side, or something else. Any
> suggestions for diagnosing or troubleshooting this?
You might need to add `gnucash_session.load()` after your call to
Session().
For background, see the thread "[GNC] unable to iterate accounts with
API Python bindings" from October 2024 at
<https://lists.gnucash.org/pipermail/gnucash-user/2024-October/113845.html>.
The crucial insight, from John Ralls:
> ...there's been only one change to the bindings since then: I removed a load call from the Session constructor to fix bug 799308. That wasn't exactly the right thing to do, but it's a surmountable problem: Load the session in the script....
I hope that is helpful. Best regards, —Jim DeLaHunt
More information about the gnucash-user
mailing list