Python [import orthanc]

Hello gents,

I feel shy to ask a second question within the same week. But I need your support to overcome this issue as well.

Although I installed python plugin, I cannot import orthanc module.
Also I need to upload an attachment to the study using .py file.

curl http://mydomain:8042/studies/58b54bac-e2f5e4c4-6329cd41-ac0844c3-3eee780e/attachments/reportPdf -X PUT --data-binary @/root/orthanc_01/etc_orthanc/testReport.pdf

I kept trying all day with no luck. any idea what else to check?
Thanks in advance our best team!



Hello Khaled,

May I ask how you are configuring Orthanc to point to your Python module?

Aren’t you trying to execute python from within your Lua script?

Hi Benjamin,

Yes, I call Python from a Lua script using the following Lua functions:

function executePythonScriptw(script, args)
local command = “python3 /etc/orthanc/lua/” … script … " ‘" … args … "’ "
local handle = io.popen(command, “r”) – Open pipe for reading
local result = handle:read(“*a”) – Read all output
handle:close()
return result
end

Here is the other part of the Lua script that calls this function:
local study = getStudyData(studyId)
local studyJson = encodeJson(study)
local result = executePythonScriptw(“30_generatePDF.py”, studyJson)
if not result or string.find(result, “Failed”) then
{the rest of the code}

I also have 30_generatePDF.py, which takes studyJson and works perfectly as long as it doesn’t interact with Orthanc. It can generate PDFs and store them in the desired directory, etc. However, when trying to use 30_generatePDF.py to access Orthanc, it requires the import orthanc module, which is where the problem starts.

Here is the related part of orthanc.conf
“Plugins”: [“/usr/local/share/orthanc/plugins”],
“LuaScripts”: [
“/etc/orthanc/lua/10_assign_newStudies.lua”,
“/etc/orthanc/lua/05_Permissions.lua”
],
“PythonScript”: “/etc/orthanc/lua/30_generatePDF.py”,
“PythonVerbose”: false,

By the way, according to the documentation, you can depend on python completely without lua, you just need to consider the event triggers in py.

Khaled,

This will not work as is. Let me explain :

Here’s how the Orthanc Python plugin interacts with your Python scripts: the plugin spins a Python interpreter and loads the script that is referenced in the Orthanc JSON configuration. Here is how you can configure the script to be loaded:

{
    "Name": "my-orthanc",
    "DicomAet": "MY_ORTHANC",
    "PythonScript": "/python/my-orthanc-plugin/my-entry-point.py",
}

The interpreter that loads the script, in addition to all the Python standard modules like os, sys, json, provides an orthanc module that can be imported, and provides functions to interact with Orthanc.

This module will not be available in scripts that are executed outside of the Orthanc Python plugin context, and that includes the scripts that are executed through Lua, like you did.

Therefore, you need to put all the Orthanc-dependent pieces of your Python scripts and reference the top-level script entry point in the Orthanc configuration, as described above (see this link)

Of course, if you have other Python code that does not depend on the orthanc module, you’re free to keep your Lua-triggered script for this purpose.

More information can be founed on the dedicated plugin page

Hope this helps! Let us know how it goes :smile:

Hi Benjamin,
It worked!!!
Many thanks dear.

1 Like

Hello @benjamin.golinvaux , thank you for the reply. It gave me a hint where my problem is - interpreter. I want to generate documentation for my python scripts but every docs generator fails because of

import orthanc

Do you have any clues how could I do it? How my pdocs pip package could use the Orthanc interpreter so that all the imports could be verified?

Thank you

For future googlers: answered here

Gist of it:

try:
    import orthanc
except ImportError:
    orthanc = None