How to import "ledger" module in Python

58 views
Skip to first unread message

Felipe Micaroni Lalli

unread,
Dec 2, 2021, 10:54:57 AM12/2/21
to ledge...@googlegroups.com
Hello guys!

I'm trying to execute this example described in ledger doc:

import ledger

if __name__ == '__main__':
    for xact in ledger.read_journal("sample.dat").xacts():
        for post in xact.posts():
            print("Transferring %s to/from %s" % (post.amount, post.account))

But the script didn't find the module "ledger". I tried to install in Ubuntu the package "python-ledger" but it still didn't work. Any ideas how to find the module? I tried to find it in the PyPi repository but with no luck. Even in pip3 I couldn't find it. I didn't find anything relevant in the Ledger repo as well, only a demo in the "python" directory.

Thank you.

David Bremner

unread,
Dec 2, 2021, 11:09:40 AM12/2/21
to Felipe Micaroni Lalli, ledge...@googlegroups.com
You may want to try the ubuntu package python3-ledger, since hopefully
you are using python3. Otherwise please let us know the versions of
ledger, python, and python-ledger. python-ledger is gone from current
debian, so I cannot test, but your script runs ok with python3-ledger
3.2.1-7+b2 on Debian.

Martin Michlmayr

unread,
Dec 2, 2021, 6:20:54 PM12/2/21
to ledge...@googlegroups.com
* Felipe Micaroni Lalli <mica...@gmail.com> [2021-12-02 12:54]:
> I'm trying to execute this example described in ledger doc:
>
> import ledger

AFAIK you have to run it is "ledger python SCRIPT".

--
Martin Michlmayr
https://www.cyrius.com/

David Bremner

unread,
Dec 2, 2021, 6:47:34 PM12/2/21
to Martin Michlmayr, ledge...@googlegroups.com
Martin Michlmayr <t...@cyrius.com> writes:

> * Felipe Micaroni Lalli <mica...@gmail.com> [2021-12-02 12:54]:
>> I'm trying to execute this example described in ledger doc:
>>
>> import ledger
>
> AFAIK you have to run it is "ledger python SCRIPT".
>

For me it works with "python3 script.py"

When I say works, it complains about the data file being missing, but
the import certainly succeeds.

d

David Bremner

unread,
Dec 4, 2021, 8:56:31 AM12/4/21
to Felipe Micaroni Lalli, ledge...@googlegroups.com
Felipe Micaroni Lalli <mica...@gmail.com> writes:

> Thank you so much. First, I had to upgrade from Debian 10 to Debian 11 to
> be able to install this package. After a lot of pain in the distro upgrade,
> I was able to install the package. But I still get the error:
>
>>
>> import ledger
>> ModuleNotFoundError: No module named 'ledger'
>>
>
> What am I missing?

Are you running the script with python3?

d

Felipe Mica

unread,
Feb 8, 2022, 7:31:08 AMFeb 8
to David Bremner, ledge...@googlegroups.com
Hi! Yes, I am using python3. Still the same error.

Ledger 3.2.1-20200518
Python 3.9.7
python-ledger:amd64/buster 3.1.2+dfsg1-1 uptodate

➜  src git:(master) ✗ python3 test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>

    import ledger
ModuleNotFoundError: No module named 'ledger'

But then, I just replaced "python3" to "python" (Python 2.7.18) and it worked fine now.

Thank you.

Now, where can I find more info about how to use this module? I didn't find any relevant or complete documentation. The official doc is very poor about that. Where can I find something more useful? Or even the source code of this module, if there is no relevant doc available?



Reply all
Reply to author
Forward
0 new messages