Python C extension: Use extension PYD or DLL?
Asked Answered
T

4

31

I have a Python extension written in C and I wonder if I should use the file extension DLL or PYD under Windows. (And what would I use in Linux?)

Are there any differences (besides the filename)?

I found an unofficial article. Is this the secret of pyc? Why can't I find any official article on this topic?

Takeover answered 24/11, 2011 at 22:3 Comment(1)
Link doesn't seem to be working, but maybe it's just my internet.Moslem
T
29

pyd files are just dll files ready for python importing.

To distinguish them from normal dlls, I recommend .pyd not .dll in windows.

Here is the official doc about this issue:

http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll

Towrope answered 12/12, 2011 at 7:23 Comment(1)
it seems that this is not just reccomendable, but obligatory. .DLLs, even with the correct PyInit_ and PyModuleDef's aren't recognised as directly importable (on python <= 3.8), although .so's are fine on linuxPullulate
C
9

According to the Creating Your Own Project step (Step 7 of "The Cookbook Approach") of Building C and C++ Extensions on Windows

The output file should be called spam.pyd (in Release mode) or spam_d.pyd (in Debug mode). The extension .pyd was chosen to avoid confusion with a system library spam.dll to which your module could be a Python interface.

So a .pyd file is just a DLL renamed to save on confusion.

On linux however, speaking from experience it seems that you need to use the .so extension for python dlls. This is just a standard unix shared library. I can't provide a source or reason for why python on linux does not change the file extension, however I can show you how to demonstrate it. At the shell, run the following:

python -vv
>>> import fakemodule

You'll notice that the output shows:

trying /usr/lib/python2.5/site-packages/fakemodule.so

Clute answered 15/12, 2011 at 13:29 Comment(0)
R
8

Presuming your Python extension foo is intended to be used as a module, accessible via import foo, you don't need to know what the filename extension should be on what operating system. You just use distutils. You will get a .pyd on Windows, and a .so on Linux etc. Read this documentation.

Update in response to comment by @gecco

import foo is working both both extension types: dll and pyd. The extension does not matter here... :

For me (Python 2.7.1, Windows 7), python -vv shows only pyd, py, pyw and pyc extensions (in that order) being searched. If I have foo.pyd in C:\python27\lib\site-packages, import foo works. If I rename that file to foo.dll, import foo fails.

Recommendation answered 24/11, 2011 at 22:46 Comment(5)
import foo is working both both extension types: dll and pyd. The extension does not matter here...Takeover
In fact I got something wrong... my fault. Unfortunately I cannot withdraw my down vote. I think I can remove it after you updated your post only...Takeover
From the official docs linked to in the accepted answer above, it states: "Note that the search path for foo.pyd is PYTHONPATH, not the same as the path that Windows uses to search for foo.dll."Concoct
@wescpy: I'm not sure whether you are agreeing or disagreeing with my answer, or even what relevance your comment has ...Recommendation
Sorry John Machin my comment was for gecco, not you. It does matter what the extension is.Concoct
S
3

In Windows you can use *.pyd to be imported directly from Python import foo but for *.dll you should use this:

from ctypes import cdll
#load dll file , the file in the same .py file location or enter the full path
mylib=cdll.LoadLibrary("foo.dll") 
#call a function from this dll (c-ext) 
ReturnedValue=mylib.FunctionName()

If you want to explore what functions are exported in this Dll use this tool

UPDATE: Here's an example using distutils and SWIG to build the extension. Check this article out, it shows many simple examples under Windows and Linux.

Scleroma answered 15/12, 2011 at 13:14 Comment(4)
You are wrong. DLL's can also be directly imported into Python using the import statement.Takeover
I'm sorry I got something wrong... (see my comment in John Machin's port) my fault.... You were right... Unfortunately I cannot withdraw my down vote. I think I can remove it after you updated your post only...Takeover
@Takeover no problem. I think this link which CHENZhao posted answers your questions about the difference and about the existence of official article. and also John Machin post helps. so all of your questions seems to be answered!!. check also my updated post.Scleroma
Yes I think that currently CHENZhao is making the race :-)Takeover

© 2022 - 2024 — McMap. All rights reserved.