如何在GnuPG Python绑定中设置GnuPG主目录?

2024-05-19 07:07:47 发布

您现在位置:Python中文网/ 问答频道 /正文

在尝试用Python初始化GnuPG绑定时,我收到一条错误消息

TypeError: __init__() got an unexpected keyword argument 'gnupghome'

这是我运行的代码:

import gnupg
gpg = gnupg.GPG(gnupghome='C:\Users\samss\AppData\Roaming\gnupg')
input_data = gpg.gen_key_input(key_type="RSA",
                           key_length=1024,
                           passphrase='n0sm@sy0')
key =gpg.gen_key(input_data)
print key

我在这里做错什么了?


Tags: keyan消息inputdatainit错误gpg
3条回答

python gnupg模块至少有3个不同版本。其中至少有两个在pip。

如果执行pip install gnupg,则会得到使用homedir参数的旧模块。

如果您这样做pip install python-gnupg,就会得到一个新的模块。在本例中,它是您正在阅读的模块的文档。

在文档方面有不同的来源,有些是homedir,有些是gnupghome。我不知道他们什么时候改的,也不知道为什么。 一些为OP提供解决方案的琐碎代码:

import gnupg
print gnupg.__version__
try:
    gpg = gnupg.GPG(gnupghome=homedir) 
except TypeError:
    gpg = gnupg.GPG(homedir=homedir)

请比较以下两个回溯。这两种情况下的代码都是一样的。一种情况下gnupg.GPG需要“homedir”,另一种情况下是“gnupghome”。 我在一个virtualenv中工作,有两个不同的gnupg分布。 在virtualenv中,python gnupg是通过pip安装的:

虚拟现实:

Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gnupg
>>> gnupg.__version__
'2.0.2'
>>> homedir=''
>>> gpg = gnupg.GPG(homedir=homedir)
>>> gpg = gnupg.GPG(gnupghome=homedir)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'gnupghome'

全球:

Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gnupg
>>> gnupg.__version__
'0.3.6'
>>> homedir=''
>>> gpg = gnupg.GPG(gnupghome=homedir)
>>> gpg = gnupg.GPG(homedir=homedir)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'homedir'

不过,我很担心杰西的gnupg版本。有人能详细说明这件事吗?

您应该参考gnupg文档,因为它使用homedir代替gnupghome

请遵循它可能解决您的问题的文档:gnupg python documentation

相关问题 更多 >

    热门问题