使用 Python gnupg 包时,出现错误 "Homedir ''C:/Users/...' needs read/write permissions"

Using the Python gnupg package, I am getting the error "Homedir ''C:/Users/...' needs read/write permissions"

我 运行ning Windows 7 在 Anaconda 中使用 Python 版本 3.6.1。我使用 pip install gnupg.

安装了 python 软件包 gnupg

我从 ftp://ftp.gnupg.org/gcrypt/binary/ 下载了 gnupg Windows 可执行文件 2.1.23。

当我 运行 下面两行中的任何一行的代码都失败并出现以下错误。

import gnupg

gpg = gnupg.GPG(binary='C:/Program Files (x86)/GnuPG/bin/gpg.exe',
               homedir='C:/Users/Alex/Desktop/SFTP Connection')

# gpg = gnupg.GPG(binary='C:/Program Files (x86)/GnuPG/bin/gpg.exe')

然而,当我 运行 带有两行中任何一行的 Python 代码时,它会失败。

import gnupg
# gpg = gnupg.GPG(homedir='C:/Program Files (x86)/GnuPG')
gpg = gnupg.GPG(homedir='C:/Program Files (x86)/GnuPG/bin')

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\gnupg\_meta.py in _homedir_setter(self, directory)
    434                 assert _util._has_readwrite(hd), \
--> 435                     "Homedir '%s' needs read/write permissions" % hd
    436             except AssertionError as ae:

AssertionError: Homedir ''C:/Users/Alex/Desktop/SFTP Connection'' needs read/write permissions

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
<ipython-input-4-ae272cb064fb> in <module>()
      1 gpg = gnupg.GPG(binary='C:/Program Files (x86)/GnuPG/bin/gpg.exe',
----> 2                homedir='C:/Users/Alex/Desktop/SFTP Connection')

C:\ProgramData\Anaconda3\lib\site-packages\gnupg\gnupg.py in __init__(self, binary, homedir, verbose, use_agent, keyring, secring, ignore_homedir_permissions, options)
    123             verbose=verbose,
    124             use_agent=use_agent,
--> 125             ignore_homedir_permissions=ignore_homedir_permissions,
    126         )
    127 

C:\ProgramData\Anaconda3\lib\site-packages\gnupg\_meta.py in __init__(self, binary, home, keyring, secring, use_agent, default_preference_list, ignore_homedir_permissions, verbose, options)
    181         self.ignore_homedir_permissions = ignore_homedir_permissions
    182         self.binary  = _util._find_binary(binary)
--> 183         self.homedir = os.path.expanduser(home) if home else _util._conf
    184         pub = _parsers._fix_unsafe(keyring) if keyring else 'pubring.gpg'
    185         sec = _parsers._fix_unsafe(secring) if secring else 'secring.gpg'

C:\ProgramData\Anaconda3\lib\site-packages\gnupg\_util.py in __set__(self, obj, value)
    763             self.fset(obj, value)
    764         else:
--> 765             getattr(obj, self.fset.__name__)(value)
    766 
    767     def __delete__(self, obj):

C:\ProgramData\Anaconda3\lib\site-packages\gnupg\_meta.py in _homedir_setter(self, directory)
    438                 log.debug("GPGBase.homedir.setter(): %s" % msg)
    439                 log.debug(str(ae))
--> 440                 raise RuntimeError(str(ae))
    441             else:
    442                 log.info("Setting homedir to '%s'" % hd)

RuntimeError: Homedir ''C:/Users/Alex/Desktop/SFTP Connection'' needs read/write permissions

我已经 运行 以下两个命令并验证了该文件夹具有读写权限。

>>> os.access('C:/Users/Alex/Desktop/SFTP Connection', os.W_OK)
True
>>> os.access('C:/Users/Alex/Desktop/SFTP Connection', os.R_OK)
True
>>>

GnuPG 主目录是 GnuPG 存储其密钥环和信息的地方。您不能使用此配置变量来确定 Python 模块在何处搜索二进制文件。事实上,这个 never 应该设置为存储二进制文件的地方(无论如何也不应该在你的程序文件目录中)。

相反,将目录添加到您的 %PATH 环境变量或使用 binary 变量,该变量应包含二进制文件的绝对路径:

gpg = gnupg.GPG(binary='C:/Program Files (x86)/GnuPG/bin/gpg.exe')

来自Python module's documentation

binary (str) – Name for GnuPG binary executable. If the absolute path is not given, the environment variable $PATH is searched for the executable and checked that the real uid/gid of the user has sufficient permissions.

homedir (str) – Full pathname to directory containing the public and private keyrings. Default is whatever GnuPG defaults to.

关于新权限错误,我猜其实是gnupg的bug,我have submitted a patch.