Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SWIG教程问题_Python_C++_Windows_Visual Studio 2015_Swig - Fatal编程技术网

Python SWIG教程问题

Python SWIG教程问题,python,c++,windows,visual-studio-2015,swig,Python,C++,Windows,Visual Studio 2015,Swig,我试图遵循swig教程,但我遇到了问题,现在我正在使用: win32上的Python 3.5.1(v3.5.1:37A07CEE59692015年12月6日01:54:25)[MSC v.1900 64位(AMD64)] Vs2015 x64,针对x64的Microsoft(R)C/C++优化编译器版本19.00.23918 SWIG版本3.0.10 内容包括: 示例.c #include <time.h> double My_variable = 3.0; int fa

我试图遵循swig教程,但我遇到了问题,现在我正在使用:

  • win32上的Python 3.5.1(v3.5.1:37A07CEE59692015年12月6日01:54:25)[MSC v.1900 64位(AMD64)]
  • Vs2015 x64,针对x64的Microsoft(R)C/C++优化编译器版本19.00.23918
  • SWIG版本3.0.10
内容包括:

示例.c

 #include <time.h>
 double My_variable = 3.0;

 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }

 int my_mod(int x, int y) {
     return (x%y);
 }

 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }
那么我会:

  • swig-python示例.i
  • cl/D\u USRDLL/D\u windell example.c example\u wrap.c-Ic:\Python351\include/link/DLL/out:example.pyd/libpath:c:\Python351\libs python35.lib
但是当我尝试
python-c“导入示例”
时,我得到了:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_example)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ImportError:动态模块未定义模块导出函数(PyInit_示例)

问题,发生了什么以及如何修复它?

SWIG的动态链接模块的名称应以下划线开头,在本例中为
\u example.pyd
。SWIG生成的Python文件正在查找名为
\u example
的模块,请参见该文件的开头:

from sys import version_info
if version_info >= (2, 6, 0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:                                           # ↓ SEE HERE
            fp, pathname, description = imp.find_module('_example', [dirname(__file__)])
        except ImportError:
            import _example # ← AND HERE
            return _example # ← AND HERE
        if fp is not None:
            try:                      # ↓ AND HERE
                _mod = imp.load_module('_example', fp, pathname, description)
            finally:
                fp.close()
            return _mod
    _example = swig_import_helper() # ← AND HERE
    del swig_import_helper
else:    # ↓ AND HERE
    import _example

实际上是由SWIG封装的C++模块的名称。

from sys import version_info
if version_info >= (2, 6, 0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:                                           # ↓ SEE HERE
            fp, pathname, description = imp.find_module('_example', [dirname(__file__)])
        except ImportError:
            import _example # ← AND HERE
            return _example # ← AND HERE
        if fp is not None:
            try:                      # ↓ AND HERE
                _mod = imp.load_module('_example', fp, pathname, description)
            finally:
                fp.close()
            return _mod
    _example = swig_import_helper() # ← AND HERE
    del swig_import_helper
else:    # ↓ AND HERE
    import _example