[英]Python: ImportError for a module in a script launched from command line
There is a custom module "ETPython" generated by SWIG (consists of ETPython.py and binary _ETPython.so ) and a simple script that invokes this module SWIG 生成了一个自定义模块“ETPython”(由ETPython.py和二进制_ETPython.so 组成)和一个调用该模块的简单脚本
import ETPython
...
There aren't any problems if the script is run in an IDE (pycharm, internal python's IDLE, squish by froglogic so on).如果脚本在 IDE(pycharm、内部 python 的 IDLE、froglogic 的挤压等)中运行,则没有任何问题。 But if I'm trying to launch it in python shell in interactive mode or via terminal using
但是,如果我尝试以交互模式或通过终端在 python shell 中使用
python3 sample.py
there is an error message like:有一条错误消息,如:
File "<path_to_file>/example.py", line 12, in <module>
import ETPython
File "<path_to_file>/ETPython.py", line 15, in <module>
import _ETPython
ImportError: dlopen(<path_to_file>/_ETPython.so, 2): Symbol not found: _NSLog
Referenced from: <path_to_file>/_ETPython.so
Expected in: flat namespace
Searching topics, I found that problem is related to wrong paths.搜索主题,我发现问题与错误的路径有关。 So I added some code:
所以我添加了一些代码:
import os, sys
os.chdir("<path_to_dir>")
sys.path.append('<path_to_dir>')
os.system('export PYTHONPATH=<path_to_dir>:$PYTHONPATH')
This code helped to import the module in python shell in interactive mode but launching in terminal is still failing.此代码有助于以交互模式在 python shell 中导入模块,但在终端中启动仍然失败。
So the question is how to make it to work?所以问题是如何让它发挥作用?
I found solution.我找到了解决方案。 The SWIG module was compiled incorrectly.
SWIG 模块编译不正确。 There was option for CMake that suppressed errors for undefined symbols
CMake 有一个选项可以抑制未定义符号的错误
set(PLATFORM_LIBS "-undefined dynamic_lookup" )
That why the module doesn't contain NSLOG symbol.这就是为什么模块不包含 NSLOG 符号的原因。 The module was recompiled with additional
该模块重新编译了额外的
"-framework Foundation"
in swig_link_libraries statement.在 swig_link_libraries 语句中。 And now the module is imported correctly
现在模块已正确导入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.