Difference between revisions of "Python Bindings"

From GnuCash
Jump to: navigation, search
(Documentation: Use URL templates, MAINT branch, mark gmane link broken)
(Use URL templates, mark broken links)
Line 16: Line 16:
  
 
See also
 
See also
* http://article.gmane.org/gmane.comp.gnome.apps.gnucash.devel/23800 "Re: documentation for python bindings" and
+
* <s>http://article.gmane.org/gmane.comp.gnome.apps.gnucash.devel/23800</s><ref name='broken_link'>Links to gmane are broken since several years. To avoid it, please link to [[Mailing Lists#Existing Lists|our mailing list archives]]!</ref> "Re: documentation for python bindings" and
* http://article.gmane.org/gmane.comp.gnome.apps.gnucash.devel/23613 "Re: documentation for python bindings" with some pointers on how to look up the existing documentation
+
* <s>http://article.gmane.org/gmane.comp.gnome.apps.gnucash.devel/23613</s><ref name='broken_link'/> "Re: documentation for python bindings" with some pointers on how to look up the existing documentation
  
 
= Setting things up =
 
= Setting things up =
  
 
;Requirements: Gnucash {{MainVersion}}.x requires Python {{Python_Version}}.
 
;Requirements: Gnucash {{MainVersion}}.x requires Python {{Python_Version}}.
:GnuCash's Python bindings has been known to work with [https://en.wikipedia.org/wiki/CPython CPython] version 3.2 or later as of GnuCash 3.0.
+
:GnuCash's Python bindings has been known to work with [{{URL:wp}}CPython CPython] version 3.2 or later as of GnuCash 3.0.
  
 
== Linux ==
 
== Linux ==
Line 36: Line 36:
 
=== Other Linux distributions / GnuCash from source ===
 
=== Other Linux distributions / GnuCash from source ===
 
;Fix me:Is it still possible since we require Python3?
 
;Fix me:Is it still possible since we require Python3?
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. <syntaxhighlight lang="sh">
+
I use {{URL:wp}}IPython IPython for an interactive shell.  If you prefer something else, let us know if there is any difference. <syntaxhighlight lang="sh">
 
export PYTHONPATH=$PYTHONPATH:/export/gnucash/lib/python{{Python_Version}}/site-packages
 
export PYTHONPATH=$PYTHONPATH:/export/gnucash/lib/python{{Python_Version}}/site-packages
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 53: Line 53:
 
== Mac OSX ==
 
== Mac OSX ==
  
Install GnuCash via [http://www.macports.org/ MacPorts]: <syntaxhighlight lang="sh">
+
Install GnuCash via [https://www.macports.org/ MacPorts]: <syntaxhighlight lang="sh">
 
sudo port install gnucash +python{{Python_Version_short}}
 
sudo port install gnucash +python{{Python_Version_short}}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 86: Line 86:
 
*** help(gnucash.gnucash_business)
 
*** help(gnucash.gnucash_business)
  
Again, <s>http://article.gmane.org/gmane.comp.gnome.apps.gnucash.devel/23613</s> contained some more hopefully useful pointers, but the page is no longer available.
+
Again, <s>http://article.gmane.org/gmane.comp.gnome.apps.gnucash.devel/23613</s><ref name='broken_link'/> contained some more hopefully useful pointers, but the page is no longer available.
  
 
= Example Usages =
 
= Example Usages =
 
This section should contain some pointers to scripts using the GnuCash Python bindings:
 
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
+
* {{URL:GH}}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
+
* {{URL:GH}}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)
+
* {{URL:GH}}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 <tt>python -Wd</tt> or with <tt>PYTHONWARNINGS=default</tt> 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.
 
;Important: GnuCash uses '''DeprecationWarnings,''' which are disabled by default in Python. Be sure to test periodically with <tt>python -Wd</tt> or with <tt>PYTHONWARNINGS=default</tt> 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.

Revision as of 09:31, 13 February 2021

Python in gnucash has two main aspects:

Python bindings provide SWIG wrapper functions for some of gnucashs C/C++ parts. 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 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. You use the python bindings in this shell. Be careful: gnucash is not designed to have multiple instances changing 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.

See also

Setting things up

Requirements
Gnucash 5.x requires Python 3.6.
GnuCash's Python bindings has been known to work with CPython version 3.2 or later as of GnuCash 3.0.

Linux

Ubuntu/Debian

The easiest way to install the GnuCash Python bindings is via apt-get:
sudo apt-get install python-gnucash

Archlinux

The python bindings are install with the gnucash package.

Other Linux distributions / GnuCash from source

Fix me
Is it still possible since we require Python3?
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.
export PYTHONPATH=$PYTHONPATH:/export/gnucash/lib/python{{Python_Version}}/site-packages

This adds the gnucash python bindings to your PYTHONPATH assuming that your gnucash resides in /export/gnucash.

You can then either start up an interactive python session with ipython or call your script to be executed:
gnucash-env ipython
gnucash-env 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 +python{{Python_Version_short}}

Note: 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/python{{Python_Version}}/site-packages
You can then either start up an interactive python session with ipython or call your script to be executed:
ipython
python /path/to/script

Documentation

As pointed out in the introductory paragraph, the documentation is rather slim at this point in time. Here are some sources where you might be able to find what you are looking for. Generally speaking, this is not yet end-user friendly stuff.

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:

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.
    Retrieved from "https://wiki.gnucash.org/wiki/index.php?title=Python_Bindings&oldid=17424"