How to compile a Python package to a dll

2019-01-16 10:36发布

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

8条回答
Evening l夕情丶
2楼-- · 2019-01-16 10:55

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.

查看更多
叛逆
3楼-- · 2019-01-16 11:04

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.

查看更多
神经病院院长
4楼-- · 2019-01-16 11:05

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 those dll 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 with py2exe and py2app (both 64 bit compatible). If you implement the interpreter, the end user's machine doesn't have to have python installed on the system.

A pyd file is the same like a dll and fully supported inside python. So you can normally import your module. You can find more information about it here.

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 with ctypes to use them (here a good post and here the python description of how it works)

查看更多
虎瘦雄心在
5楼-- · 2019-01-16 11:09

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?

查看更多
唯我独甜
6楼-- · 2019-01-16 11:11

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

查看更多
相关推荐>>
7楼-- · 2019-01-16 11:14

Grab Visual Studio Express and IronPython and do it that way? You'll be in Python 2.7.6 world though.

查看更多
登录 后发表回答