0

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.

Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
  • What does `sys.path` look like just before the `ImportError`? AFAIK placing the directory in the `PYTHONPATH` is everything you have to do, so maybe you didn't add the directory correctly. – Bakuriu May 07 '13 at 07:10
  • Sys.path = [..., 'D:\\02_DEV\\EVOLAB\\EVOLAB-2013-05-02\\ECLIPSE-WS_ELP\\evolab-cython\\src\\Spatial', 'D:\\02_DEV\\EVOLAB\\EVOLAB-2013-05-02\\ECLIPSE-WS_ELP\\evolab-cython\\setup', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', ...] Where 'D:\\02_DEV\\EVOLAB\\EVOLAB-2013-05-02\\ECLIPSE-WS_ELP\\evolab-cython\\setup' is the location of my .pyd. And I checked the "myext.cpp" generated by Cython which contains the required "PyMODINIT_FUNC initmyext(void); /*proto*/" instruction. So the path seems to be ok. May be my .pyd file is not well compiled. What do you think ? – Gauthier Boaglio May 07 '13 at 07:28
  • Are you using Eclipse? Did you try to run the program from the command prompt instead of inside Eclipse? – Bakuriu May 07 '13 at 07:31
  • Yes, I am using Eclipse, but I tried the direct command line too... – Gauthier Boaglio May 07 '13 at 07:40
  • Did you try to import your module from the directory where it is located? i.e. `cd D:\\02_DEV etc.; python import myext`. – Bakuriu May 07 '13 at 08:03
  • Got it ! My extension had dependencies itself (dll's) that I forgot to append to my PATH env var. Now it seems to work. You were right by saying that adding the pyd to the PYTHONPATH was sufficient, so many thanks for having conforted me in that direction. – Gauthier Boaglio May 07 '13 at 08:13

1 Answers1

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.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231