【问题标题】:Embedded python interpreter fails to import c modules嵌入式 python 解释器无法导入 c 模块
【发布时间】:2016-08-18 15:56:43
【问题描述】:

MinGW-w64 项目 (https://sourceforge.net/p/mingw-w64/discussion/723798/thread/4a8a9ed5/?limit=25) 的 gdborig.exe 中的嵌入式 python 解释器存在问题。

模块 itertools 和其他几个模块的导入失败。但是使用分布式独立 python 解释器,相应的导入工作正常:

>>> import sys
>>> print sys.version
2.7.9 (default, Jul 11 2016, 16:32:13)
[GCC 6.1.0]
>>> print sys.executable
C:/AUEMARK/Programme/MinGW64/mingw64/opt/bin/python.exe
>>> import itertools
>>> itertools
<module 'itertools' from 'C:\AUEMARK\Programme\MinGW64\mingw64\opt\libpython2.7\lib-dynload/itertools.pyd'>

使用嵌入式 python 解释器:

(gdb) python import sys
(gdb) python print sys.version
2.7.9 (default, Jul 11 2016, 16:32:13)
[GCC 6.1.0]
(gdb) python print sys.executable
C:/AUEMARK/Programme/MinGW64/mingw64/opt/bin/python.exe
(gdb) python import itertools
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named itertools
Error while executing Python code.

可以:

  • 导入系统
  • 导入操作系统

不能:

  • 导入迭代工具
  • 导入集合
  • ...

导入集合的错误消息

(gdb) python import collections
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\AUEMARK\Programme\MinW64\mingw64\opt\lib\python2.7/collections.py", line 8, in <module>
    from _collections import deque, defaultdict
Importerror: no module named _collections
Error while executing Python code.

所以看起来嵌入式解释器无法在C中导入模块。Python模块被导入,C模块的Python部分也可以被嵌入式解释器访问。

感谢任何提示,我该如何解决这个问题。

马库斯

【问题讨论】:

    标签: python-2.7 gdb windows-7-x64 mingw-w64


    【解决方案1】:

    我认为存在此问题是因为未找到文件 itertools.pyd。 我必须设置环境变量:

    PYTHONPATH=C:\msys64\mingw64\lib\python2.7;C:\msys64\mingw64\lib\python2.7\lib-dynload;
    

    在您的情况下,请确保您设置:

    PYTHONPATH=C:/AUEMARK/Programme/MinGW64/mingw64/opt/lib/python2.7;C:\AUEMARK\Programme\MinW64\mingw64\lib\python2.7\lib-dynload
    

    我的 PYTHONHOME 变量指向 python 解释器:

    PYTHONHOME=C:\msys64\mingw64\bin\pyhon.exe
    

    我在 msys2 下构建了自己的 gdb,它可以工作:

    Microsoft Windows [Version 10.0.16299.19]
    (c) 2017 Microsoft Corporation. All rights reserved.
    
    C:\Users\tiit>C:\msys64\home\tiit\gdb-7.11-bin\bin\x86_64-linux-gnu-gdb.exe
    GNU gdb (GDB) 7.11.1.20160801-git
    Copyright (C) 2016 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later
    <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "--host=x86_64-w64-mingw32 --target=x86_64-linux-gnu".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>.
    Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.
    For help, type "help".
    Type "apropos word" to search for commands related to "word".
    (gdb)
    

    如果没有设置正确的环境变量,我也会遇到同样的问题。

    【讨论】:

      猜你喜欢
      • 2017-03-05
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多