[GNC] unable to iterate accounts with API Python bindings

pudgier_fruit.0v at icloud.com pudgier_fruit.0v at icloud.com
Fri Oct 25 00:51:40 EDT 2024


Hi,

     I've been trying to get started with the GnuCash API Python bindings
after just using the main desktop app for the last 12 years.
I built GnuCash app (Build ID: 5.9+(2024-09-28) along with API and Python
bindings enabled for my system (MacOS 15.0 , M1 Arm)

My problem is that I haven't been able to interact with my GnuCash data in
a useful way yet.  Namely, I can't seem to access any existing accounts,
and thus no transactions either.  Nor any entries in the price database.

First I used the desktop app to save-as a copy of my GnuCash data in the
XML format.
Next I use python and  commands generally like the following.  I do not see
any output that is expected when iterating over the descendants of
root_account.  ( I never see the text output, 'checking a descendant...' )


from gnucash import (Session, Account, Transaction, Split, GncNumeric,
SessionOpenMode, gnucash_core)
uri="file:///Users/me/Downloads/gnucash_localSave.gnucash"
session = Session(book_uri=uri,
mode=gnucash_core.SessionOpenMode.SESSION_NORMAL_OPEN )
book = session.book
table = book.get_table()
currency_code = 'USD'
currency = table.lookup('ISO4217', currency_code)
root_account = book.get_root_account()
for acc in root_account.get_descendants():
    print('checking a descendant...')
    if acc.GetType() == ACCT_TYPE_STOCK:
        print('found a stock')


**So why is root_account.get_descendants() returning an empty container?**

The Bindings/API aren't completely dead though.  With the same session and
book open above, I was playing around with some of the ideas Eberhard
demonstrated in this thread [
https://lists.gnucash.org/pipermail/gnucash-user/2024-September/113402.html]
and I called
>>> create_accounts(session, book, root_acct)
>>> quit()

To my suprise, when I used the desktop app to open
gnucash_localSave.gnucash, my real accounts and transactions are gone, but
I have three accounts: Expenses, Opening Balance, and Savings in CAD
currency, and with the specified opening balances.
So the Bindings and API worked to create stuff.

As an aside, the file did flag as locked upon open in the desktop app.  I
suspect there's a missing unlock somewhere in Python Bindings / destructor
code.

Cheers,

Noah


More information about the gnucash-user mailing list