Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python C extension: Use extension PYD or DLL?

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?

like image 489
gecco Avatar asked Nov 24 '11 22:11

gecco


People also ask

Is a pyd file the same as a DLL?

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

What is needed for C extension?

Building C and C++ Extensions with distutils. Extension modules can be built using distutils, which is included in Python. Since distutils also supports creation of binary packages, users don't necessarily need a compiler and distutils to install the extension.

What is Python extension?

The Python extension supports code completion and IntelliSense using the currently selected interpreter. IntelliSense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.


2 Answers

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

like image 131
zchenah Avatar answered Oct 11 '22 16:10

zchenah


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

like image 35
obmarg Avatar answered Oct 11 '22 16:10

obmarg