Difference between revisions of "Python Bindings"
m (→Prepare the environment: Newlines) |
|||
Line 48: | Line 48: | ||
== Prepare the environment == | == Prepare the environment == | ||
− | If you are using a distribution package, you are probably all set and can skip this step. Similarly if you build gnucash and install it in the default system location (/usr/local), what follows is probably not necessary. However if you are building from source (on any platform) but install into a non-default location, or don't install at all you will need to set up a few environment variables to help gnucash and python to find each other and their dependencies (like where gnucash can find its gsettings schema). | + | If you are using a distribution package, you are probably all set and can skip this step. Similarly if you build gnucash and install it in the default system location (/usr/local), what follows is probably not necessary. |
+ | |||
+ | However if you are building from source (on any platform) but install into a non-default location, or don't install at all you will need to set up a few environment variables to help gnucash and python to find each other and their dependencies (like where gnucash can find its gsettings schema). | ||
=== Build and install === | === Build and install === | ||
Line 85: | Line 87: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
;Note: <q>3.6</q> should match the python version used to build GnuCash. | ;Note: <q>3.6</q> should match the python version used to build GnuCash. | ||
− | |||
− | |||
==Interactive Shell== | ==Interactive Shell== |
Revision as of 07:33, 17 October 2023
Python in gnucash has two main aspects:
- Python bindings
- Python shell
Python bindings provide SWIG wrapper functions for some of gnucashs C/C++ API. They can be used to write standalone scripts to work with the gnucash financial data. In the source tree they are located at bindings/python.
The python shell on the other hand (if enabled) opens at gnucash startup and provides a python environment to use with the running gnucash instance. In the source tree it is located at gnucash/python. The python bindings can be used in this shell. Be careful: gnucash is not designed to have multiple instances manipulating the data at the same time, reading should be secure.
Note, that there is also a pure python client, piecash, to read and manipulate GnuCash books if saved in of the three SQL backends (sqlite, MySQL, Postgres). Currently (Oct 2020), piecash supports files created up to GnuCash 4.2.
General notice
Python bindings have been added to gnucash in 2008 or earlier. There is still very little documentation and probably few people would know how to use it. While not a manual, this page was created in the hope to provide information for those interested in gnucash and python. Python bindings are not installed on Windows.
See also
-
http://article.gmane.org/gmane.comp.gnome.apps.gnucash.devel/23800[1] "Re: documentation for python bindings" and -
http://article.gmane.org/gmane.comp.gnome.apps.gnucash.devel/23613[1] "Re: documentation for python bindings" with some pointers on how to look up the existing documentation - Perhaps it was this thread: documentation for python bindings
Setting things up
Python package
A Python package must be installed:
- Gnucash 5.x
- requires Python ≥ 3.6.
- GnuCash 3.x
- GnuCash's Python bindings has been known to work with CPython version 3.2 or later as of GnuCash 3.0.
GnuCash
On the other hand one needs a GnuCash package with python bindings enabled. Most linux distributions either build GnuCash with these bindings enabled by default or provide it via a separate package, usually named python3-gnucash or something similar. For example:
- Enabled by default
- The GnuCash packages in Archlinux and Fedora have been built with the python bindings enabled by default.
- Split in a separate package
- In Debian based distributions like Ubuntu the easiest way to install the GnuCash Python bindings is via apt-get:
sudo apt-get install python3-gnucash
- Building from source
- It is also possible to use python bindings when building gnucash from source yourself. In this case you need to make sure the bindings are enabled while configuring the build, for example with
cmake -DWITH_PYTHON=ON $OTHER_OPTIONS
- Notes
- - If you are bulding GnuCash yourself see CMake#GnuCash Configuration Variables for the OTHER_OPTIONS.
- - The flatpaks we create currently don't ship with python bindings enabled
- - Similarly we don't ship python bindings with our Windows and MacOS packages. It may be possible to build Windows and MacOS packages yourself based on our Windows and MacOS build scripts (by enabling python bindings during the build), but I have not tested this.
Prepare the environment
If you are using a distribution package, you are probably all set and can skip this step. Similarly if you build gnucash and install it in the default system location (/usr/local), what follows is probably not necessary.
However if you are building from source (on any platform) but install into a non-default location, or don't install at all you will need to set up a few environment variables to help gnucash and python to find each other and their dependencies (like where gnucash can find its gsettings schema).
Build and install
These apply when you have built and installed gnucash from source.
In what follows you should set INST_DIR to the path where gnucash is installed during your build. This is often referred to as the installation prefix or something similar.
INST_DIR=xyz # Point this to the actual directory gnucash was installed in !!
PYTHONPATH=$INST_DIR/lib64/python3.6/site-packages:$PYTHONPATH
XDG_DATA_DIRS=$INST_DIR/share:${XDG_DATA_DIRS}:/usr/local/share:/usr/share
export PYTHONPATH XDG_DATA_DIRS
# More path settings may be needed to interface with the guile bindings (if that's possible) or finance::quote
# You can check prefix/etc/gnucash/environment for the full list the gnucash sets up for itself
- Note
-
3.6
should match the python version used to build GnuCash.
User:Kohtala notes that if even if you install to the default CMAKE_INSTALL_PREFIX of /usr/local, the Ubuntu/Debian python is compiled to search for packages in dist-packages and does not find them in site-packages. So you may have to change the PYTHONPATH line above accordingly.
Build
It is also possible to use the python bindings from the gnucash build directory. During development that saves an extra install step for each code iteration. However this needs you to set up two more parameters in addition to those set up when gnucash is installed and we need to point at the build directory instead of the install directory.
In what follows you should set BUILD_DIR to the path where gnucash is built. This is the directory in which cmake and make/ninja are executed.
BUILD_DIR=xyz # Point this to the actual directory gnucash was installed in !!
PYTHONPATH=$BUILD_DIR/lib64/python3.6/site-packages:$PYTHONPATH
XDG_DATA_DIRS=$BUILD_DIR/share:${XDG_DATA_DIRS}:/usr/local/share:/usr/share
GNC_UNINSTALLED=1
GNC_BUILDDIR=$BUILD_DIR
export PYTHONPATH XDG_DATA_DIRS GNC_UNINSTALLED GNC_BUILDDIR
# More path settings may be needed to interface with the guile bindings (if that's possible) or finance::quote
# You can check prefix/etc/gnucash/environment for the full list the gnucash sets up for itself
- Note
-
3.6
should match the python version used to build GnuCash.
Interactive Shell
I use https://en.wikipedia.org/wiki/IPython IPython for an interactive shell. If you prefer something else, let us know if there is any difference.If you have followed the setup steps above you can either start up an interactive python session with ipython or call your script to be executed: <syntaxhighlight lang="sh">
ipython
python /path/to/script
- Notes
- To install ipython run
sudo pip ipython
- pip is the package installer for Python.
Mac OSX
- Install GnuCash via MacPorts:
sudo port install gnucash +python36
- Notes
-
-
36
should now be36
. - at the time of writing, only the non-quartz environment is supported.
-
- Setup the PYTHONPATH to point to your macports install:
export PYTHONPATH=$PYTHONPATH:/opt/local/lib/python3.6/site-packages
- Note
-
3.6
should now be3.6
.
ipython
python /path/to/script
Documentation
As pointed out in the introductory paragraph, the documentation is rather slim at this point in time. Below you will find some sources where you might be able to find what you are looking for. Generally speaking, this is not yet end-user friendly stuff.
Principles
Python bindings provide SWIG wrapper functions for some of gnucashs C/C++ API. They can be used to write standalone scripts to work with the gnucash financial data.
What SWIG does
How to use the Python bindings
Further Docs
Doxygen
Have a look at the python-bindings (bugfix branch) or python-bindings (feature branch) in the doxygen API documentation.
Github
The source code of gnucash including the python bindings can be accessed on github.
- Example scripts from the source code.
Inside python
Using python or ipython there is some sparse help:
big long list of stuff
import gnucash.gnucash_core_c help(gnucash.gnucash_core_c) dir(gnucash.gnucash_core_c)
higher abstraction level help
import gnucash.gnucash_core import gnucash.gnucash_business help(gnucash.gnucash_core) help(gnucash.gnucash_business)
Mailing list
Again, http://article.gmane.org/gmane.comp.gnome.apps.gnucash.devel/23613[1] contained some more hopefully useful pointers, but the page is no longer available.
Example Usages
This section should contain some pointers to scripts using the GnuCash Python bindings:
- https://github.com/hjacobs/gnucash-qif-import --- imports transactions from QIF text files into GnuCash's own file format
- https://github.com/hjacobs/gnucash-stock-portfolio --- script using the GnuCash commodity/price table
- https://github.com/hjacobs/gnucash-fiximports --- tries to fix target accounts using regular expression rules (e.g. PIZZA -> Expenses:Dining)
- Important
- GnuCash uses DeprecationWarnings, which are disabled by default in Python. Be sure to test periodically with python -Wd or with PYTHONWARNINGS=default set in the environment so that you know about API that will disappear in the next major release. See Development_Process#LibGnuCash API Stability for the policy.
- ↑ 1.0 1.1 1.2 Links to gmane are broken since several years. To avoid it, please link to our mailing list archives!