1

我最近了解了 Python 运行压缩目录的能力,并开始尝试它。这对于我测试过的小型纯 Python 程序非常有效。但是,一旦我使用基于 C 的库迁移到更大的应用程序,事情就会变得如此。我有一个使用密码学的应用程序,但我无法让它工作。以下是如何产生我看到的错误:

这是__main__文件:

# cat __main__.py 
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
backend = default_backend()
key = os.urandom(32)
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
ct = encryptor.update(b"a secret message") + encryptor.finalize()
decryptor = cipher.decryptor()
print(decryptor.update(ct) + decryptor.finalize())

这是创建压缩文件的方法:

#!/bin/bash
set -e
rm -R build/
mkdir build
cp __main__.py build/
pip2 install -v -U --no-cache-dir --target=build/ cryptography
cd build/
zip -r ../test.pyz .

现在我得到了一个test.pyz我正在尝试运行的文件,但出现以下错误:

$ python2 test.pyz
Traceback (most recent call last):
  File "/usr/lib64/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "test.pyz/__main__.py", line 4, in <module>
  File "/tmp/tmpZjvIXl/lib/python/cryptography/hazmat/backends/__init__.py", line 35, in default_backend
  File "/tmp/tmpZjvIXl/lib/python/cryptography/hazmat/backends/__init__.py", line 22, in _available_backends
  File "/tmp/pip-build-3JjP5c/setuptools/pkg_resources/__init__.py", line 2235, in resolve
  File "/tmp/tmpZjvIXl/lib/python/cryptography/hazmat/backends/openssl/__init__.py", line 7, in <module>
  File "/tmp/tmpZjvIXl/lib/python/cryptography/hazmat/backends/openssl/backend.py", line 15, in <module>
  File "/tmp/tmpZjvIXl/lib/python/cryptography/x509/__init__.py", line 7, in <module>
  File "/tmp/tmpZjvIXl/lib/python/cryptography/x509/base.py", line 15, in <module>
  File "/tmp/tmpZjvIXl/lib/python/cryptography/x509/extensions.py", line 19, in <module>
  File "/tmp/tmpZjvIXl/lib/python/cryptography/hazmat/primitives/constant_time.py", line 9, in <module>
ImportError: No module named _constant_time

有谁知道如何在 zip 文件中使用基于 C 的模块?

更新

正如建议的那样,我尝试使用 zipapp 打包应用程序并使用 Python3 运行它,但我仍然看到同样的错误。

更新 2

这是我失败的完整日志:

$ pip3.5 install -v -U --no-cache-dir --target=build/ pycryptodome
$ cat build/__main__.py 

from Crypto.Cipher import AES

def main():
    print(bla)

$ python3.5 -m zipapp build
$ python3.5 build.pyz 
Traceback (most recent call last):
  File "/usr/lib64/python3.5/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "build.pyz/__main__.py", line 2, in <module>
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
  File "build.pyz/Crypto/Cipher/__init__.py", line 78, in <module>
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load

bal = AES

  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
  File "build.pyz/Crypto/Cipher/_mode_ecb.py", line 46, in <module>
  File "build.pyz/Crypto/Util/_raw_api.py", line 168, in load_pycryptodome_raw_lib
OSError: Cannot load native module 'Crypto.Cipher._raw_ecb'

不知道如何在压缩目录中使用基于 C 的模块...

4

0 回答 0