嵌入式python解释器无法导入c模块

时间:2016-08-18 15:56:43

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

我在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.

可以:

  • import sys
  • import os

不能:

  • import itertools
  • 导入集合
  • ...

导入集合的错误消息

(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部分。

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

马库斯

1 个答案:

答案 0 :(得分:0)

我认为存在此问题是因为找不到文件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)

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