Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Cython to create a stand dll

Tags:

python

dll

I have a Python script, I renamed the script to .pyx file. I want compile this code to a stand dll file.

I saw in this document that Cython will create a dll file, but i only get a pyd.

I have mingw and try to use command python setup.py build --compiler=mingw32 to compile the script my code (just a hello world):

def init():
    return "hello world"

Any ideas? Thanks

like image 700
ljx0517 Avatar asked May 29 '11 21:05

ljx0517


People also ask

Can I make DLL with Python?

Python embedding is supported in CFFI version 1.5, you can create a . dll file which can be used by a Windows C application.

Does Cython create executable?

It does link to a simple working example. To compile the Cython source code to a C file that can then be compiled to an executable you use a command like cython myfile. pyx --embed and then compile with whichever C compiler you are using.

Is Cython as fast as C?

Cython is the same speed as a carefully tuned C/C++ program; carefully tuned, Cython maps directly to C/C++. I've done many benchmarks of low level numerical code when implementing SageMath (which uses Cython for several 100K lines of code).

What can you do with Cython?

At its core, Cython is an intermediate step between Python and C/C++. It allows you to write pure Python code with some minor modifications, which is then translated directly into C code.


2 Answers

Is a *.pyd file the same as a DLL?

Yes, .pyd files are dll’s, but there are a few differences. If you have a DLL named foo.pyd, then it must have a function PyInit_foo(). You can then write Python “import foo”, and Python will search for foo.pyd (as well as foo.py, foo.pyc) and if it finds it, will attempt to call PyInit_foo() to initialize it. You do not link your .exe with foo.lib, as that would cause Windows to require the DLL to be present.

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.

Modifying Python’s Search Path, Absolute and Relative Imports

like image 184
Cees Timmerman Avatar answered Oct 16 '22 11:10

Cees Timmerman


So the first thing to do is rename the file to helloworld.pyx. Now we need to make the setup.py, which is like a python Makefile (for more information see Compilation). Your setup.py should look like:

from distutils.core import setup
from distutils.extension import Extension

from Cython.Distutils import build_ext
    setup(
        cmdclass = {'build_ext': build_ext},
        ext_modules = [Extension("helloworld",
    ["helloworld.pyx"])] )

To use this to build your Cython file use the commandline options:

$ python setup.py build_ext --inplace

Which will leave a file in your local directory called helloworld.so in unix or helloworld.dll in Windows.

Now to use this file: start the python interpreter and simply import it as if it was a regular python module:

like image 27
Jakob Bowyer Avatar answered Oct 16 '22 09:10

Jakob Bowyer