How to set the GnuPG home directory within the GnuPG Python binding?

会有一股神秘感。 提交于 2019-11-30 15:41:24

问题


When trying to initialize the GnuPG binding in Python, I'm getting an error message

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

This is the code I'm running:

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

What am I doing wrong here?


回答1:


You should refer to the gnupg documentation as it uses homedir at the place of gnupghome.

Please follow the documentation it might solve your issues: gnupg python documentation




回答2:


There are differnet sources when it comes to documentation, some with homedir, some with gnupghome. I dont know the time they changed it or why. Some trivial code to provide a solution to OP:

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

Please compare the two following tracebacks. Its the same code in both cases. In one case gnupg.GPG expects 'homedir and in the other case 'gnupghome'. I am working in a virtualenv and have two different distributions of gnupg. In the virtualenv python gnupg was installed via pip:

virtualenv:

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'

global:

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'

I am concerned by the old gnupg version in jessie, though. Can someone elaborate on the matter?




回答3:


There are at least 3 different versions of the python gnupg module. At least 2 of them are in pip.

If you do pip install gnupg, you get the older module that uses the homedir argument.

If you do pip install python-gnupg, you get a newer module. In this case, it was the documentation for that module you were reading.




回答4:


GnuPG (as command line tool) knows two ways to specify the GnuPG home directory:

  1. Using an environment variable:

    GPGHOME=C:\Users\samss\AppData\Roaming\gnupg gpg
    
  2. Passing it as a parameter (which is also available as homedir parameter in the configuration file):

    gpg --homedir=C:\Users\samss\AppData\Roaming\gnupg
    

The GnuPG Python binding allows some parameters to be passed during initialization. Because of the initialization syntax, you probably mixed this up with the environment variable version of defining the home directory on the command line.

An additional warning: You probably want to access another system user's GnuPG home directory. GnuPG is very picky about safe permissions. For a development machine, it might be fine to use the GnuPG parameter --no-permission-warning and privileges that allow rather broad access, but better start with a clean approach from the start and initialize a new GnuPG home directory for your application that can be properly restricted regarding permissions.



来源:https://stackoverflow.com/questions/35028852/how-to-set-the-gnupg-home-directory-within-the-gnupg-python-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!