Я пытаюсь использовать пакет pyomo для выполнения некоторых вычислений в сложном приложении C ++. Модуль C ++, в котором работает встроенный интерпретатор Python, является общей библиотекой, которая используется в качестве плагина основным приложением. Основное приложение вызывает плагин используя dlopen / dlsym.
Среда :
-Дебианский 10,3 x86_64;
- Python v.3.7.3 (по умолчанию);
-G CC v.8.3.0.
Ошибка возникает в момент импорта подпакета pyomo.environ:
#include <Python.h>
...
Py_Initialize();
PyRun_SimpleString("import pyomo.environ");
После выполнения PyRun_SimpleString("import pyomo.environ");
string я вижу следующий вывод:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/pyomo/environ/__init__.py", line 69, in _import_packages
_do_import(pname)
File "/usr/local/lib/python3.7/dist-packages/pyomo/environ/__init__.py", line 16, in _do_import
importlib.import_module(pkg_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/usr/local/lib/python3.7/dist-packages/pyomo/common/__init__.py", line 27, in <module>
from ._task import pyomo_api, PyomoAPIData, PyomoAPIFactory
File "/usr/local/lib/python3.7/dist-packages/pyomo/common/_task.py", line 22, in <module>
import pyutilib.workflow
File "/usr/local/lib/python3.7/dist-packages/pyutilib/workflow/__init__.py", line 17, in <module>
from pyutilib.workflow.executable import ExecutableResource
File "/usr/local/lib/python3.7/dist-packages/pyutilib/workflow/executable.py", line 5, in <module>
import pyutilib.services
File "/usr/local/lib/python3.7/dist-packages/pyutilib/services/__init__.py", line 10, in <module>
from pyutilib.services.services import registered_executable, register_executable, TempfileManager
File "/usr/local/lib/python3.7/dist-packages/pyutilib/services/services.py", line 13, in <module>
from pyutilib.component.config import TempfileManager
File "/usr/local/lib/python3.7/dist-packages/pyutilib/component/config/__init__.py", line 22, in <module>
from pyutilib.component.config.logging_config import LoggingConfig
File "/usr/local/lib/python3.7/dist-packages/pyutilib/component/config/logging_config.py", line 14, in <module>
import logging.handlers as handlers
File "/usr/lib/python3.7/logging/handlers.py", line 28, in <module>
import queue
File "/usr/lib/python3.7/queue.py", line 16, in <module>
from _queue import Empty
ImportError: /usr/lib/python3.7/lib-dynload/_queue.cpython-37m-x86_64-linux-gnu.so: undefined symbol: PyExc_MemoryError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python3.7/dist-packages/pyomo/environ/__init__.py", line 97, in <module>
_import_packages()
File "/usr/local/lib/python3.7/dist-packages/pyomo/environ/__init__.py", line 81, in _import_packages
raise ImportError(msg)
ImportError: pyomo.environ failed to import pyomo.common.plugins:
Original ImportError: /usr/lib/python3.7/lib-dynload/_queue.cpython-37m-x86_64-linux-gnu.so: undefined symbol: PyExc_MemoryError
Traceback:
File "/usr/local/lib/python3.7/dist-packages/pyomo/environ/__init__.py", line 69, in _import_packages
_do_import(pname)
File "/usr/local/lib/python3.7/dist-packages/pyomo/environ/__init__.py", line 16, in _do_import
importlib.import_module(pkg_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/usr/local/lib/python3.7/dist-packages/pyomo/common/__init__.py", line 27, in <module>
from ._task import pyomo_api, PyomoAPIData, PyomoAPIFactory
File "/usr/local/lib/python3.7/dist-packages/pyomo/common/_task.py", line 22, in <module>
import pyutilib.workflow
File "/usr/local/lib/python3.7/dist-packages/pyutilib/workflow/__init__.py", line 17, in <module>
from pyutilib.workflow.executable import ExecutableResource
File "/usr/local/lib/python3.7/dist-packages/pyutilib/workflow/executable.py", line 5, in <module>
import pyutilib.services
File "/usr/local/lib/python3.7/dist-packages/pyutilib/services/__init__.py", line 10, in <module>
from pyutilib.services.services import registered_executable, register_executable, TempfileManager
File "/usr/local/lib/python3.7/dist-packages/pyutilib/services/services.py", line 13, in <module>
from pyutilib.component.config import TempfileManager
File "/usr/local/lib/python3.7/dist-packages/pyutilib/component/config/__init__.py", line 22, in <module>
from pyutilib.component.config.logging_config import LoggingConfig
File "/usr/local/lib/python3.7/dist-packages/pyutilib/component/config/logging_config.py", line 14, in <module>
import logging.handlers as handlers
File "/usr/lib/python3.7/logging/handlers.py", line 28, in <module>
import queue
File "/usr/lib/python3.7/queue.py", line 16, in <module>
from _queue import Empty
Пояснение : эта проблема появляется, только если интерпретатор работает внутри динамически загружаемой общей библиотеки. Если я запускаю код python непосредственно из python или из интерпретатора, встроенного в автономное тестовое приложение, все работает должным образом.