2016-10-03 112 views
1

我試圖按照痛飲教程,但我卡住了,現在我使用:痛飲教程問題

  • 的Python 3.5.1(V3.5.1:37a07cee5969,2015年12月6日, 1點54分25秒)[MSC v.1900 64位(AMD64)]在Win32
  • Vs2015 64,微軟(R)C/C++優化編譯器版19.00.23918針對x64
  • SWIG版3.0.10

內容爲:

example.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); 
} 

example.i

%module example 
%{ 
/* Put header files here or function declarations like below */ 
extern double My_variable; 
extern int fact(int n); 
extern int my_mod(int x, int y); 
extern char *get_time(); 
%} 

extern double My_variable; 
extern int fact(int n); 
extern int my_mod(int x, int y); 
extern char *get_time(); 

然後我做的:

  • swig -python example.i
  • cl /D_USRDLL /D_WINDLL example.c example_wrap.c -Ic:\Python351\include /link /DLL /out:example.pyd /libpath:c:\python351\libs python35.lib

但是當我嘗試python -c "import example"我得到:

Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
ImportError: dynamic module does not define module export function (PyInit_example) 

問,這是怎麼回事,如何解決呢?

回答

1

SWIG的動態鏈接模塊的名稱應以下劃線開頭,在本例中爲_example.pyd。該SWIG生成的Python的文件尋找一個名爲_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++模塊的名稱。