Compatability Tools

Some packages require a little extra help to work nicely from PythonCall.jl.

Some of these are "fixes" that are silently applied for you, and some are just extra functions to bridge a gap. We aim to keep these as minimal as possible.

Stdlib

Whenever a Python exception is displayed by Julia, sys.last_traceback and friends are set. This allows the post-mortem debugger pdb.pm() to work. Disable by setting PythonCall.CONFIG.sysautolasttraceback = false.

Tabular data & Pandas

A pandas.DataFrame can be wrapped in Julia as a PyPandasDataFrame, providing a Tables.jl-compatible interface.

In the other direction, the following functions can be used to convert any Tables.jl-compatible table to a Python table.

PythonCall.pycolumntableFunction
pycolumntable([T=PyObject,] src) :: T

Construct a "column table" from the Tables.jl-compatible table src, namely a Python dict mapping column names to column vectors.

PythonCall.pyrowtableFunction
pyrowtable([T=PyObject,] src) :: T

Construct a "row table" from the Tables.jl-compatible table src, namely a Python list of rows, each row being a Python dict mapping column names to values.

PythonCall.pypandasdataframeFunction
pypandasdataframe([T=PyObject,] [src]; ...) :: T

Construct a pandas dataframe from src.

Usually equivalent to pyimport("pandas").DataFrame(src, ...), but src may also be Tables.jl-compatible table.

MatPlotLib / PyPlot

PythonCall.pyplotshowFunction
pyplotshow([fig]; close=true, [format])

Show the matplotlib/pyplot/seaborn/etc figure fig, or all open figures if not given, using Julia's display mechanism.

If close is true, the figure is also closed.

The format specifies the file format of the generated image. By default this is pyplot.rcParams["savefig.format"]. It can be one of "png", "jpg", "jpeg", "tif", "tiff", "svg" or "pdf".

If Julia is running an IJulia kernel, pyplotshow() is automatically called after executing a cell, so that plots generated in a cell are always shown (similar to IPython). It can be disabled by setting PythonCall.CONFIG.pyplotautoshow = false.

GUIs (including MatPlotLib)

Event loops

If for example you wish to use PyPlot in interactive mode (matplotlib.pyplot.ion()) then activating the correct event loop will allow it to work.

PythonCall.event_loop_onFunction
event_loop_on(g::Symbol; interval=40e-3, fix=false)

Activate an event loop for the GUI framework g, so that the framework can run in the background of a Julia session.

The event loop runs every interval seconds. If fix is true and g is a Qt framework, then fix_qt_plugin_path is called.

Supported values of g (and the Python module they relate to) are: :pyqt4 (PyQt4), :pyqt5 (PyQt5), :pyside (PySide), :pyside2 (PySide2), :gtk (gtk), :gtk3 (gi), :wx (wx), :tkinter (tkinter).

Interaction

The following is an alternative to using event loops to enable interactive plotting.

PythonCall.pyinteractFunction
pyinteract(; force=false, sleep=0.1)

Some Python GUIs can work interactively, meaning the GUI is available but the interactive prompt is returned (e.g. after calling matplotlib.pyplot.ion()). To use these from Julia, currently you must manually call pyinteract() each time you want to interact.

Internally, this is calling the PyOS_InputHook asynchronously. Only one copy is run at a time unless force is true.

The asynchronous task waits for sleep seconds before calling the hook function. This gives time for the next prompt to be printed and waiting for input. As a result, there will be a small delay before the GUI becomes interactive.

Qt path fix

PythonCall.fix_qt_plugin_pathFunction
fix_qt_plugin_path()

Try to set the QT_PLUGIN_PATH environment variable in Python, if not already set.

This fixes the problem that Qt does not know where to find its qt.conf file, because it always looks relative to sys.executable, which can be the Julia executable not the Python one when using this package.

If CONFIG.qtfix is true, then this is run automatically before PyQt4, PyQt5, PySide or PySide2 are imported.

IPython

If Python is running an IPython kernel, then:

  • Julia's Base.stdout is set to Python's sys.stdout.
  • An IPythonDisplay is pushed onto Julia's display stack, so that display(x) goes to IPython if possible.