Python plugin: how to use external libraries (provided by pip) ?

Hello all,

I’m still trying to use the Python plugin and thanks to the help provided by Sebastien Jodogne recently I can access the DICOM fields I want in Python.
Now, I’d like to use an external library that I have installed through Python pip (PyPi).

This command show that kafka-python is installed:

root@8c2ea060f3cc:~# pip install kafka-python
Requirement already satisfied: kafka-python in /usr/local/lib/python2.7/dist-packages (2.0.1)

But when I try to start Orthanc configured with a Python script it show this error:

E0622 16:47:38.144973 PluginsManager.cpp:164] Error during the installation of the Python script, traceback:
<class ‘ModuleNotFoundError’>
No module named ‘kafka’
File “/etc/orthanc/onStoredInstanceCallback.py”, line 7, in
from kafka import KafkaProducer
E0622 16:47:38.145017 PluginsManager.cpp:102] Error while initializing plugin /usr/local/share/orthanc/plugins/libOrthancPython.so (code -1)

In fact I do not even know which version (2 or 3) is used by Orthanc !

Would messing with the PYTHONPATH variable help me ?

Best regards
François

Hi,

If you’re using Docker to run Orthanc, for instance starting for the osimis/orthanc image, then you need to install the PyPi package into the image so it can be found by Orthanc.
Sample code (not tested but inspired from other Dockerfile files used at Osimis - I’m not sure installing python3 is actually required - to be confirmed):

`

FROM osimis/orthanc:20.5.3

install system prerequisites

RUN apt-get update && apt-get install -yq python3 python3-pip

configure the terminal in utf-8 for the python logging to work correctly

RUN locale-gen “en_US.UTF-8”
ENV LC_ALL=en_US.UTF-8

install prerequisites for the python app

RUN pip3 install kafka-python

`

I hope this helps.

Cheers,

Michel

Hi,

Python3 and pip are actually already installed in osimis/orthanc images.

You can check this sample:
https://bitbucket.org/osimis/orthanc-setup-samples/src/master/docker/python/

Alain.

Hello,

Each precompiled binaries of the Python plugin is bound to a specific version of the Python interpreter (most probably 2.7, 3.7 or 3.8).

You can very easily find the version of the Python interpreter using by your Orthanc plugin by running the following script through the Orthanc plugin:

import sys
print(sys.version)
print(sys.path)

On my Ubuntu 18.04 box, I get the following answer if running this script:

W0627 11:01:17.209013 PluginsManager.cpp:168] Python plugin is initializing
W0627 11:01:17.209076 PluginsManager.cpp:168] Force global loading of Python shared library: /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0

2.7.17 (default, Nov 7 2019, 10:07:09)

[GCC 7.4.0]
[‘/usr/lib/python2.7’, ‘/usr/lib/python2.7/plat-x86_64-linux-gnu’, ‘/usr/lib/python2.7/lib-tk’, ‘/usr/lib/python2.7/lib-old’, ‘/usr/lib/python2.7/lib-dynload’, ‘/home/jodogne/.local/lib/python2.7/site-packages’, ‘/usr/local/lib/python2.7/dist-packages’, ‘/usr/lib/python2.7/dist-packages’, ‘/usr/lib/python2.7/dist-packages/gtk-2.0’]

This clearly indicates that I use Python 2.7.

As an alternative, here is a oneliner to find the version of the Python installation for the Osimis docker images without starting Orthanc thanks to the “ldd” program:

$ docker run --rm -t -i --entrypoint=/usr/bin/ldd osimis/orthanc:20.5.3 /usr/share/orthanc/plugins-disabled/libOrthancPython.so
[…]
libpython3.7m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.7m.so.1.0 (0x00007f00c836f000)

This shows that the Docker image 20.5.3 uses Python 3.7.

HTH,
Sébastien-