【发布时间】:2020-09-29 12:22:15
【问题描述】:
有一个由 SWIG 生成的自定义模块“ETPython”(由 ETPython.py 和二进制 _ETPython.so 组成)和一个调用该模块的简单脚本 p>
sample.py
import ETPython
...
如果脚本在 IDE 中运行(pycharm、内部 python 的 IDLE、froglogic 的 squish 等),则不会出现任何问题。但是,如果我尝试在 python shell 中以交互模式或通过终端使用
启动它python3 sample.py
有如下错误信息:
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
搜索主题,我发现问题与错误的路径有关。所以我添加了一些代码:
import os, sys
os.chdir("<path_to_dir>")
sys.path.append('<path_to_dir>')
os.system('export PYTHONPATH=<path_to_dir>:$PYTHONPATH')
此代码有助于在 python shell 中以交互模式导入模块,但在终端中启动仍然失败。
那么问题是如何让它发挥作用?
【问题讨论】:
-
代替
sys.path.append('<path_to_dir>'),试试sys.path.insert(0, '<path_to_dir>') -
如果您有自定义路径,请查看
sitecustomize.py -
@inspectorG4dget,它没有帮助
标签: python macos terminal swig