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
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.
You can use pyinstaller for converting the .py files into executable with all required packages into .dll format.
Step 1. pip install pyinstaller,
step 2. new python file let's name it code.py .
step 3. Write some lines of code i.e print("Hello World")
step 4. Open Command Prompt in the same location and write pyinstaller code.py hit enter. Last Step see in the same location two folders name build, dist will be created. inside dist folder there is folder code and inside that folder there is an exe file code.exe along with required .dll files.
I would also using Cython to generate
pyd
files, like Dikei wrote. But if you really want to secure your code, you should better write the important stuff in C++. The best would be to combine both C++ and Python. The idea: you would leave the python code open for adjustments, so that you don't have to compile everything over and over again. That means, you would write the "core" in C++ (which is the most secure solution these days) and use thosedll
files in your python code. It really depends what kind of tool or program you are building and how you want to execute it. I create mostly an execution file (exe
,app
) once I finish a tool or a program, but this is more for the end user. This could be done withpy2exe
andpy2app
(both 64 bit compatible). If you implement the interpreter, the end user's machine doesn't have to have python installed on the system.Using and generating
pyd
files is the fastest and easiest way to create safe and portable python code.You could also write real
dll
files in C++ and import them withctypes
to use them (here a good post and here the python description of how it works)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?
Python embedding is supported in CFFI version 1.5, you can create a
.dll
file which can be used by a Windows C application.Grab Visual Studio Express and IronPython and do it that way? You'll be in Python 2.7.6 world though.