python 如何保护Python源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2055355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:40:18  来源:igfitidea点击:

How to protect Python source code?

pythonccompilationbytecode

提问by Nathan Osman

Is it possible to distribute only the bytecode version (.pyc file) of a Python script instead of the original .py file? My app embeds the Python interpreter and calls PyImport_Import to load a script. How can I tell it to look for a .pyc file and import that?

是否可以只分发 Python 脚本的字节码版本(.pyc 文件)而不是原始的 .py 文件?我的应用程序嵌入了 Python 解释器并调用 PyImport_Import 来加载脚本。我怎么能告诉它寻找一个 .pyc 文件并导入它?

采纳答案by Micha? Niklas

I did it by creating .py library and simple .py program that uses that library. Then I compiled library to .pyc and distributed: program as .py source and library as compiled .pyc.

我通过创建 .py 库和使用该库的简单 .py 程序来做到这一点。然后我将库编译为 .pyc 并分发:program as .p​​y source and library ascompiled .pyc。

回答by Michel Gokan

Use the freezetool, which is included in the Python source tree as Tools/freeze. It converts Python byte code to C arrays; with a C compiler you can embed all your modules into a new program, which is then linked with the standard Python modules.

使用冻结工具,它作为Tools/freeze包含在 Python 源代码树中。它将 Python 字节码转换为 C 数组;使用 C 编译器,您可以将所有模块嵌入到一个新程序中,然后与标准 Python 模块链接。

Note that freeze requires a C compiler.

请注意,冻结需要 C 编译器。

Other utilities:

其他实用程序:

1- PyInstaller

1- PyInstaller

2- Py2Exe

2- Py2Exe

3- Squeeze

3-挤压

4- cx_freeze

4- cx_freeze

more info on effbot.org

有关 effbot.org 的更多信息

回答by Ned Batchelder

Since you are writing your main program in C++, you can do whatever you want to protect your Python files. You could encrypt them for distribution, then decrypt them just in time to import them into the Python interpreter, for example.

由于您使用 C++ 编写主程序,因此您可以为保护 Python 文件做任何想做的事情。例如,您可以加密它们以进行分发,然后及时解密它们以将它们导入 Python 解释器。

Since you are using PyImport_Import, you can write your own __import__hook to import the modules not from a file but from a memory buffer, so your transformation to .pyc file can happen all in memory, with no understandable Python code on disk at all.

由于您使用的是 PyImport_Import,您可以编写自己的__import__钩子来导入模块,而不是从文件而是从内存缓冲区,因此您到 .pyc 文件的转换可以全部发生在内存中,而磁盘上根本没有可理解的 Python 代码。

回答by Ber

This should not be a problem, if you create a stand alone program using py2exeyou only get the .pyc files.

这应该不是问题,如果您使用py2exe创建独立程序,您只能获得 .pyc 文件。

Normally you don't need to tell python to look for .pyc files, it does so anyway. Only if there is a newer .py source file this is used.

通常你不需要告诉 python 查找 .pyc 文件,它无论如何都会这样做。仅当有更新的 .py 源文件时才使用它。

However, the level of protection of you source code may not be very high.

但是,您源代码的保护级别可能不是很高。

回答by Bernd Haug

In the interactive interpreter, that's automatic - if there is no .py, the .pyc will still be used:

在交互式解释器中,这是自动的 - 如果没有 .py,仍将使用 .pyc:

$ echo 'print "hello"' > test.py
$ python -m compileall .
$ rm test.py
$ python -m test
hello
$

Could you just try if that works the same way with the API?

您可以尝试一下,如果这与 API 的工作方式相同吗?

Edited to add: I agree with Ber in that your code protection will be rather weak. -O will remove docstrings, if that doesn't change the behaviour of your programme, that may make reconstructing behaviour harder, but what you'd really need some sort of bytecode obfuscation.

编辑补充:我同意 Ber 的观点,因为您的代码保护将相当薄弱。-O 将删除文档字符串,如果这不会改变您程序的行为,那可能会使重构行为变得更加困难,但是您确实需要某种字节码混淆。

I don't know if a ready-made obfuscation tool exists for python, but thissounds viable, if you want to / can invest the time (and don't feel all too silly doing it, andcan ship your own interpreter).

我不知道是否有现成的混淆工具为Python,但这个声音可行的,如果你想/可以投资的时间(不要觉得太傻了这样做,并且可以运送自己的解释)。