Using cython extension once compiled [under Windows], how to use .pyd's?

余生颓废 提交于 2019-12-11 20:42:36

问题


I am used with compiling self-made python extensions under Linux using Cython. On Linux, I use distutils to produce a "myext.so", that I can then simply add to my PYTHONPATH and get the import myext python call available.

Now, I am trying to get it working under Windows 7 (using Cython 0.18). I successfully ran distutils such that I now get a myext.pyd file. But it seems (http://docs.python.org/2/faq/windows.html#is-a-pyd-file-the-same-as-a-dll) that adding the path to "myext.pyd" is not enough under windows. What should the "myext.py" look like or, in other words, what is the procedure to get my extension available in my Python (2.7) installation.

Note : After having added the directory containing "myext.pyd" to the PYTHONPATH, I still get :

python BdmLsim4.py -i model.xml
Traceback (most recent call last):
  File "BdmLsim4.py", line 6, in <module>
    import myext
ImportError: DLL load failed: module not found.

Many thanks.


回答1:


It's clearly written in the documentation you linked:

Note that the search path for foo.pyd is PYTHONPATH, not the same as the path that Windows uses to search for foo.dll. Also, foo.pyd need not be present to run your program, whereas if you linked your program with a dll, the dll is required. Of course, foo.pyd is required if you want to say import foo. In a DLL, linkage is declared in the source code with __declspec(dllexport). In a .pyd, linkage is defined in a list of available functions.

So you should either place your .pyd file inside the python's installation directories(site-packages), or you can modify the environmental variable PYTHONPATH and add the directory where the .pyd is placed.

Yet an other alternative is to use .pth files to extend the PYTHONPATH.



来源:https://stackoverflow.com/questions/16412764/using-cython-extension-once-compiled-under-windows-how-to-use-pyds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!