How to compile a Python package to a dll

115,450

Solution 1

Write everything you want to hide in Cython, and compile it to pyd. That's as close as you can get to making compiled python code.

Also, dll is not a standard, not in Python world. They're not portable, either.

Solution 2

Nowadays a simple solutino exists: use Nuitka compiler as described in Nuitka User Manual

Use Case 2 - Extension Module compilation If you want to compile a single extension module, all you have to do is this:

 python -m nuitka --module some_module.py 

The resulting file some_module.so can then be used instead of some_module.py.

You need to compile for each platform you want to support and write some initialization code to import so/pyd file ~~appropriate for given platform/python version etc.~~

[EDIT 2021-12]: Actually in python 3 the proper so/dll is determined automatically based on the file name (if it includes python version and platform - can't find PEP for this feature at the moment but Nuitka creates proper names for compiled modules). So for python 2.7 the library name would be something.pyd or something.so whereas for python 3 this would change to something.cp36-win32.pyd or something.cpython-36m-x86_64-linux-gnu.so (for 32bit python 3.6 on x86).

The result is not DLL as requested but Python-native compiled binary format (it is not bytecode like in pyc files; the so/pyd format cannot be easily decompiled - Nuitka compiles to machine code through C++ translation)

EDIT [2020-01]: The compiled module is prone to evaluation methods using python standard mechanisms - e.g. it can be imported as any other module and get its methods listed etc. To secure implementation from being exposed that way there is more work to be done than just compiling to a binary module.

Solution 3

You can embed python inside C. The real trick is converting between C values and Python values. Once you've done that, though, making a DLL is pretty straightforward.

However, why do you need to make a dll? Do you need to use this from a non-python program?

Solution 4

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

Solution 5

You can use py2exe.org to convert python scripts into windows executables. Granted this will only work on windows, but it's better then nothing.

Share:
115,450
Developer
Author by

Developer

Developer def Lover(Programming): return ['Love']*len(Programming) print Lover(['Python','Fortran','VB','Matlab',[]])

Updated on February 12, 2022

Comments

  • Developer
    Developer about 2 years

    Well, I have a Python package. I need to compile it as dll before distribute it in a way easily importable. How? You may suggest that *.pyc. But I read somewhere any *.pyc can be easily decompiled!

    Update: Follow these:
    1) I wrote a python package
    2) want to distribute it
    3) do NOT want distribute the source
    4) *.pyc is decompilable >> source can be extracted!
    5) dll is standard