Python/C API and QT to use OVITO in a graphical C++ application

I wrote some python script to make my life easier and use ovito to perform some simple simulation data evaluation. For the rest of my project I only used C++ (and qt5).
The script looks something like this:

import ovito
from ovito.modifiers import …
from ovito.io import …

#version check, script was designed for OVITO 3.5.2
print("OVITO Version: %i.%i.i" ovito.version)

To make it work with the rest of my project I call the script by the python/c API:

// using Python/C API Python/C API Reference Manual — Python 3.9.5 documentation with OVITO’s python interface
#include <Python.h>

#include

int porosity(){
Py_Initialize();
PyRun_SimpleString(“import ovito”);

PyRun_SimpleString("print(“OVITO Version: %i.%i.i\" ovito.version)”);

return 0;
}
Surprisingly this works like a charm. But if I try to run it in a QT project

import ovito

seems to cause problems. Has anyone tried something similar and has a working solution? I know that I didn’t really give much infos, probably there is something wrong with linking the libraries to the qt project…

Hi,
Which variant of the OVITO Python module are you trying to import? The one that was installed by the pip install command from the PyPI repository?

OVITO itself is based on the Qt framework. The standalone OVITO Python module has PySide2 as a dependency, which ships with its own copy of the Qt libraries. You are probably using your own copy of the Qt libraries for your C++ application, and this could lead to conflicts.

Can you say some more about the problems that occur when you import the OVITO module? Please first check if import PySide2.QtGui works before you try to import ovito.

exactly, I used

python -m pip install -U ovito

print("OVITO Version: %i.%i.i" ovito.version) says it is version 3.5.2

The error occurring:
Traceback (most recent call last):

File “”, line 1, in

ImportError: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.15’ not found (required by /home/user/.local/lib/python3.8/site-packages/PySide2/QtGui.abi3.so)

Traceback (most recent call last):

File “”, line 1, in

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/init.py”, line 19, in

from .plugins.PyScript import (version, version_string, gui_mode, headless_mode, Scene, scene, dataset, enable_logging)

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/plugins/init.py”, line 12, in

import PySide2.QtCore

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

ImportError: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.15’ not found (required by /home/user/.local/lib/python3.8/site-packages/PySide2/QtCore.abi3.so)

Traceback (most recent call last):

File “”, line 1, in

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/init.py”, line 19, in

from .plugins.PyScript import (version, version_string, gui_mode, headless_mode, Scene, scene, dataset, enable_logging)

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/plugins/init.py”, line 12, in

import PySide2.QtCore

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

ImportError: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.15’ not found (required by /home/user/.local/lib/python3.8/site-packages/PySide2/QtCore.abi3.so)

Traceback (most recent call last):

File “”, line 1, in

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/init.py”, line 19, in

from .plugins.PyScript import (version, version_string, gui_mode, headless_mode, Scene, scene, dataset, enable_logging)

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/plugins/init.py”, line 12, in

import PySide2.QtCore

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

ImportError: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.15’ not found (required by /home/user/.local/lib/python3.8/site-packages/PySide2/QtCore.abi3.so)

Traceback (most recent call last):

File “”, line 1, in

NameError: name ‘ovito’ is not defined

import PySide2.QtGui did not work and seems to produce the same result:
Traceback (most recent call last):

File “”, line 1, in

File “/home/user/.local/lib/python3.8/site-packages/ovito/init.py”, line 19, in

from .plugins.PyScript import (version, version_string, gui_mode, headless_mode, Scene, scene, dataset, enable_logging)

File “/home/user/.local/lib/python3.8/site-packages/ovito/plugins/init.py”, line 12, in

import PySide2.QtCore

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

ImportError: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.15’ not found (required by /home/user/.local/lib/python3.8/site-packages/PySide2/QtCore.abi3.so)

Traceback (most recent call last):

File “”, line 1, in

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/init.py”, line 19, in

from .plugins.PyScript import (version, version_string, gui_mode, headless_mode, Scene, scene, dataset, enable_logging)

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/plugins/init.py”, line 12, in

import PySide2.QtCore

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

ImportError: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.15’ not found (required by /home/user/.local/lib/python3.8/site-packages/PySide2/QtCore.abi3.so)

Traceback (most recent call last):

File “”, line 1, in

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/init.py”, line 19, in

from .plugins.PyScript import (version, version_string, gui_mode, headless_mode, Scene, scene, dataset, enable_logging)

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

File “/home/user/.local/lib/python3.8/site-packages/ovito/plugins/init.py”, line 12, in

import PySide2.QtCore

File “/home/user/.local/lib/python3.8/site-packages/shiboken2/files.dir/shibokensupport/feature.py”, line 142, in _import

return original_import(name, *args, **kwargs)

ImportError: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.15’ not found (required by /home/user/.local/lib/python3.8/site-packages/PySide2/QtCore.abi3.so)

Traceback (most recent call last):

File “”, line 1, in

NameError: name ‘ovito’ is not defined

I think the following happens:

Your C++ application loads /usr/lib/x86_64-linux-gnu/libQt5Core.so.5 at startup, which must be some older version of the Qt framework (<5.15). Importing the ovito module implicitly imports the PySide2 module, which was built against Qt 5.15. Normally, when running in a standalone Python interpreter, PySide2 would now load the Qt 5.15 shared libraries that are packaged with PySide2. But because your host application has already loaded another version of the Qt libraries into the process, you get a symbol lookup error (“version Qt_5.15 not found”).

You can try to build your C++ application against the copy of the Qt framework packaged with the PySide2 Python module. Or at least some other copy of the Qt libs, which has the same version number as the one shipping with PySide2. That’s how the OVITO Python module was built, which also uses Qt directly as well as indirectly through PySide2.

In case this approach is not successful, I would switch to an Anaconda or Miniconda environment. Install our OVITO Anaconda package, which pulls in Pyside2 and a full copy of the Qt framework. All components are compatible here, and it should be straightforward to build your C++ application against the right library version.