【问题标题】:Getting "OSError: Unable to run gpg () - it may not be available." error with python-gnupg获取“OSError:无法运行 gpg () - 它可能不可用。” python-gnupg 出错
【发布时间】:2019-10-19 13:02:50
【问题描述】:

我正在尝试制作一个简单的工具来解密 GPG 消息,但解密消息时一直出错,我不知道问题出在哪里。

代码如下:

import gnupg
import os

message = '''-----BEGIN PGP MESSAGE-----
Version: GnuPG v2
jA0ECQMCVady3RUyJw3X0kcBF+zdkfZOMhISoYBRwR3uk3vNv+TEg+rJnp4/yYIS
pEoI2S82cDiCNBIVAYWB8WKPtH2R2YSussKhpSJ4mFgqyOA01uwroA==
=KvJQ
-----END PGP MESSAGE----- 
'''

passphrase = 'topsecret'

gpg = gnupg.GPG(os.popen("which gpg").read().strip())

decrypted_data = str(gnupg.decrypt(message, passphrase=passphrase))

print(decrypted_data) 

这是错误:

PermissionError: [Errno 13] Permission denied: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 29, in <module>
    gpg = gnupg.GPG(os.popen("which gpg").read().strip())
  File "/Users/***/PycharmProjects/untitled8/venv/lib/python3.6/site-packages/gnupg.py", line 849, in __init__
    raise OSError(msg)
OSError: Unable to run gpg () - it may not be available.

【问题讨论】:

    标签: python gnupg


    【解决方案1】:

    python-gnupg 软件包要求您安装有效的 gpg 可执行文件。

    来自Deployment Requirements 文档:

    除了足够新的 Python 版本,为了使用它 您需要访问兼容版本的 GnuPG 的模块 可执行。系统已在 Windows 上使用 GnuPG v1.4.9 进行测试 和 Ubuntu。在 Linux 平台上,这通常通过以下方式安装 您的发行版的包管理器(例如 Debian/Ubuntu 上的 apt-get)。 Windows 二进制文件可用here – 使用其中一种 gnupg-w32cli-1.4.x.exe 安装程序以获得最简单的部署选项。

    你得到的错误很清楚:

    OSError: Unable to run gpg () - 它可能不可用。

    来自这部分:

    os.popen("which gpg").read().strip()
    

    尝试从终端/控制台运行which gpg。如果您没有安装gpgwhich gpg 将评估为空字符串'',然后整行将评估为空'' 字符串。它实际上与

    gpg = gnupg.GPG('')
    

    这将引发相同的错误,即找不到gpg 可执行文件。

    所以,基本上,你需要先安装gpg

    如果您已经安装了gpg,但由于某种原因which gpg 找不到它,您也可以传递gpgbinary parameter

    gpg = gnupg.GPG(gpgbinary="/usr/local/bin/gpg")
    

    【讨论】:

    • 谢谢!我需要做的一个小改动是将“gpg”更改为“gpg2”。 gpg = gnupg.GPG(gpgbinary="/usr/local/bin/gpg2")
    猜你喜欢
    • 2022-12-28
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多