Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Extending with Python

49 views
Skip to first unread message

Igbanam Ogbuluijah

unread,
Feb 6, 2025, 5:01:52 PMFeb 6
to Ledger
Hi all,

I'm trying to create some Python wrapper to work with my ledger files. I see that there's the Extending with Python; but I don't know from what package I should import ledger. I looked through PyPi and… doesn't seem like any package there points to a ledger-cli extension. I also tried running the python/demo.py in the repository. This rightly fails to import ledger as well

$ python demo.py   # with pyt...@3.13
Traceback (most recent call last):
  File "/Users/igbanam/projects/igbanam/ledger/python/demo.py", line 10, in <module>
    import ledger
ModuleNotFoundError: No module named 'ledger'

Please help point me in the right direction.

Thank you


Igbanam

Igbanam Ogbuluijah

unread,
Feb 8, 2025, 5:42:27 PMFeb 8
to Ledger
I figured this out. Turns out Boost Python was my unknown-unknown here. There still is no turnkey way to work with ledger in Python, but it is possible. This is what I did

  • Clone the ledger repository locally gh repo clone ledger/ledger
  • cd ledger
  • mkdir build — this collects all the build artefacts if present; it's a cleaner way than building in the repository root
  • run ./acprep --python dependencies to ensure ledger dependencies are installed
    • if this does not automatically install boost-python, ensure you have it
  • run ./acprep --compiler=c++ --python --ninja
    • I'm on a mac, hence c++ because of some libraries from Apple
    • I kept the --python tag cos that's the end goal here
    • and --ninja is a sweet quiet incremental make
  • Copy the built library into your Python project — cp build/ledger/debug/ledger.so path/to/your/python-project`

With this done, you can then import ledger and follow along with everything in the Extending with Python section of the documentation. One way to test this is to copy python/demo.py to the build directory and run it. It should run fine.

Do I wish there was an easier way to do this, so we can unlock the innovation of smart Python folks looking to build extensions for ledger and ending up defaulting to other tools? Yes, I do; but I lack the python-fu and c++-fu to get this done — please help contribute if you can.


Igbanam

Alexis

unread,
Feb 12, 2025, 1:52:51 PMFeb 12
to ledge...@googlegroups.com
Hello Igbanam

your message inspired me to take up work again on adding support
for installing ledger through pypi.

The work is *very* much work in progress, yet I've successfully
built the ledger Python module using scikit-build-core.

If you or anyone else wanted to take a peek you can do so over at:
https://github.com/afh/ledger/tree/ledger-pypi

There is more work to do to fully support the invocation of the Python
REPL via ledger python and there might be remaining issues with
inline Python statements in journals.

I'll continue my work, and appreciate any ideas and feedback folks
may have.


Best
Alexis
Reply all
Reply to author
Forward
0 new messages