有没有办法将跨越不同文件的python项目代码库合并到一个文件中? [英] Is there a way to combine a python project codebase that spans across different files into one file?

查看:108
本文介绍了有没有办法将跨越不同文件的python项目代码库合并到一个文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做的原因是我想使用工具 pyobfuscate 来混淆我的 python 代码.但是pyobfuscate只能混淆一个文件.

The reason I want to this is I want to use the tool pyobfuscate to obfuscate my python code. Butpyobfuscate can only obfuscate one file.

推荐答案

我已经单独回答了你的直接问题,但让我提供一个不同的解决方案来解决我怀疑你实际上正在尝试做的事情:

I've answered your direct question separately, but let me offer a different solution to what I suspect you're actually trying to do:

与其传送混淆的源代码,不如传送字节码文件.这些是自动创建、缓存和使用的 .pyc 文件,但您也可以仅使用 compileall 标准库中的模块.

Instead of shipping obfuscated source, just ship bytecode files. These are the .pyc files that get created, cached, and used automatically, but you can also create them manually by just using the compileall module in the standard library.

缺少 .py 文件的 .pyc 文件可以很好地导入.它不是人类可读的.它当然可以反编译为 Python 源代码,但结果是……与在原始源代码上运行混淆器得到的结果基本相同.因此,它比您尝试做的要好一些,而且要容易得多.

A .pyc file with its .py file missing can be imported just fine. It's not human-readable as-is. It can of course be decompiled into Python source, but the result is… basically the same result you get from running an obfuscater on the original source. So, it's slightly better than what you're trying to do, and a whole lot easier.

您无法以这种方式编译顶级脚本,但这很容易解决.只需编写一个单行包装脚本,它除了 import 真正的顶级脚本之外什么都不做.如果您有 if __name__ == '__main__': 代码,您还需要将其移动到一个函数中,并且包装器变成了一个两行,导入模块并调用该函数… 但这也太难了.)或者,您可以只在顶级脚本上运行 pyobfuscator,但实际上,没有理由这样做.

You can't compile your top-level script this way, but that's easy to work around. Just write a one-liner wrapper script that does nothing but import the real top-level script. If you have if __name__ == '__main__': code in there, you'll also need to move that to a function, and the wrapper becomes a two-liner that imports the module and calls the function… but that's as hard as it gets.) Alternatively, you could run pyobfuscator on just the top-level script, but really, there's no reason to do that.

事实上,许多打包工具可以选择自动为您完成所有这些工作,除了编写微不足道的顶级包装器.例如,默认的 py2app 构建会将您自己模块的编译版本以及您依赖的 stdlib 和站点包模块粘贴到 pythonXY.zip 文件中应用程序包,并设置嵌入式解释器以使用该 zip 文件作为其标准库.

In fact, many of the packager tools can optionally do all of this work for you automatically, except for writing the trivial top-level wrapper. For example, a default py2app build will stick compiled versions of your own modules, along with stdlib and site-packages modules you depend on, into a pythonXY.zip file in the app bundle, and set up the embedded interpreter to use that zipfile as its stdlib.

这篇关于有没有办法将跨越不同文件的python项目代码库合并到一个文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆