简体   繁体   English

Python:从命令行启动的脚本中模块的导入错误

[英]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 组成)和一个调用该模块的简单脚本

sample.py样本.py

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.

相关问题 从 launchd 启动的 Python 脚本给出 ImportError: No module named foo - Python script launched from launchd gives ImportError: No module named foo 如何将参数传递给从命令行启动的 python gdb 脚本 - How to pass arguments to a python gdb script launched from command line Python(Windows)将通过命令行打开文件,但不会从eclipse启动的脚本中打开文件 - Python (windows) will open files from command line, but not from a script launched from eclipse 命令行中的快速python脚本与在IDLE shell中启动 - speed python script on command line vs launched in IDLE shell python 库从命令行加载并正常工作,但如果我尝试在启动时运行,则会出现“ImportError: No module” - python library loads and works properly from command line, but 'ImportError: No module' if I try to run at boot Python 串行模块在命令行中工作,但不能从脚本中工作 - Python serial module works in command line but not from script 导入python模块以使脚本能够从命令行运行 - Importing a python module to enable a script to be run from command line 从命令行运行 python 脚本时找不到模块 - module not found when python script run from command line 从命令行运行 python 脚本时未找到模块错误 - module not found error when python script is run from command line ImportError 和 ModuleNotFoundError:如何让脚本从命令行运行? - ImportError and ModuleNotFoundError: how to get a script running from the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM