如何从受密码保护的 zip 运行 python 脚本

How to run a python script from a password protected zip

我知道 python modules/scripts 可以 运行 来自 zip 存档,如下所述: https://www.python.org/dev/peps/pep-0441/https://blogs.gnome.org/jamesh/2012/05/21/python-zip-files/

但我的问题是:

是否可以使用密码保护此存档或 .pyz 文件并 运行 使用另一个小 python 脚本来发送密码,然后 运行 __main__.py ?

谢谢!

管理导入的代码在 zipimport 模块中。 读取存档是在函数 getdata 中完成的 它假定文件通过解压缩未加密(参见 here)。

所以不,不幸的是,似乎无法直接从命令行使用加密文件。但是你可以想象一个包装器通过使用 importlib (an example).

来做到这一点

我自己需要这样的东西,所以我做到了。您需要在此处找到的模块:https://github.com/Dakkaron/ArchiveImporter

那你就可以这样使用了:

python ArchiveImporter.py [zipfile] [-p=password] [args...]

适用于 Python2 和 Python3。

该模块也可以从代码中使用:

# First import the ArchiveImporter module
import ArchiveImporter
# Then add the password encrypted file you want to import from using addZip(zippath, password)
ArchiveImporter.addZip("test.pyz", "password")
# Now import modules from the archive as usual
import testmod