python - 使用 python-gnupg 获取 "OSError: Unable to run gpg () - it may not be available."错误

标签 python gnupg

我正在尝试制作一个简单的工具来解密 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软件包要求您安装 gpg可执行。

来自 Deployment Requirements文档:

Apart from a recent-enough version of Python, in order to use this module you need to have access to a compatible version of the GnuPG executable. The system has been tested with GnuPG v1.4.9 on Windows and Ubuntu. On a Linux platform, this will typically be installed via your distribution’s package manager (e.g. apt-get on Debian/Ubuntu). Windows binaries are available here – use one of the gnupg-w32cli-1.4.x.exe installers for the simplest deployment options.



你得到的错误很明显:

OSError: Unable to run gpg () - it may not be available.



它来自这一部分:
os.popen("which gpg").read().strip()

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

然后将引发相同的错误,即找不到 gpg可执行。

所以,基本上,你需要安装 gpg第一的。

如果您已经拥有 gpg安装并出于某种原因 which gpg就是找不到,你也可以传一个 gpgbinary parameter :
gpg = gnupg.GPG(gpgbinary="/usr/local/bin/gpg")

关于python - 使用 python-gnupg 获取 "OSError: Unable to run gpg () - it may not be available."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463965/

相关文章:

python - 你如何在 Peewee 模型中存储列表?

python - 将 pandas 数据框的每一列替换为数组的每个值

python - 有没有办法在 plotly express 中实现堆叠或分组条形图

node.js - 使用 Nodejs (Crypto) 加密文件会引发错误 : bad base64 decode

python - 如何使用 python3 pickle 读取 python2 cPikle 的序列化数据?

python - 从 python BeautifulSoup 的输出中删除新行 '\n'

go - 无法解密二进制文件

android - 已安装 GnuPG,但来自 gradle 任务的 "command not found"

Ansible:如何从文件中导入 GPG 私钥?

maven - 尝试在 settings.xml 中加密 gpg 密码