I'm trying to verify a file signature, however when creating my gnupg object using
gpg = gnugp.GPG(gnupghome='/Users/myname/.gnupg')
However I keep getting a no such file or directory error. I've also tried different paths for the home, as well as not including a path and letting it use the default, all to no avail.
Some of the functions do not need anything beyond the GPG home directory (i.e. ~/.gnupg) specified, but others require a little more than that. Especially if you're manipulating keyrings.
I got around this by making a config.py file in the same directory as my other scripts which contains this:
homedir = "/Users/username"
gpg_home = homedir+"/.gnupg"
gpg_homeshort = "~/.gnupg" # optional
pub_ring = gpg_home+"/pubring.gpg"
sec_ring = gpg_home+"/secring.gpg"
pring = []
sring = []
pring.append(pub_ring)
sring.append(sec_ring)
That's for OS X, for Linux, BSD and other UNIXes change the first line to:
homedir = "/home/username"
For scripts with basic encryption and decryption, this should work:
import gnupg
from config import *
gpg = gnupg.GPG(gnupghome=gpg_home)
With the verbose parameter this results in:
>>> gpg = gnupg.GPG(gnupghome=gpg_home, verbose=True)
gpg --status-fd 2 --no-tty --homedir /Users/username/.gnupg --version
>>>
The shorter form results in:
>>> gpg = gnupg.GPG(gnupghome=gpg_homeshort, verbose=True)
gpg --status-fd 2 --no-tty --homedir '~/.gnupg' --version
>>>
For scripts which search through the default keyrings, this should work:
import gnupg
from config import *
gpg = gnupg.GPG(gnupghome=gpg_home, keyring=pring, secret_keyring=sring)
gpg.encoding = "latin-1"
pkeys = gpg.list_keys(False)
skeys = gpg.list_keys(True)
Note the change of character encoding, this is to prevent errors when reading some keyrings which may contain strange keys (which may or may not break the scripts if you leave it as "UTF-8"). This will result in pkeys and skeys being created as lists with each entry containing a dict for each key in the keyring.
The verbose form results in:
>>> gpg = gnupg.GPG(gnupghome=gpg_home, keyring=pring, secret_keyring=sring, verbose=True)
gpg --status-fd 2 --no-tty --homedir /Users/username/.gnupg --no-default-keyring --keyring /Users/username/.gnupg/pubring.gpg --secret-keyring /Users/username/.gnupg/secring.gpg --version
>>>
The short form results in:
>>> gpg = gnupg.GPG(gnupghome=gpg_homeshort, keyring=pring, verbose=True)
gpg --status-fd 2 --no-tty --homedir '~/.gnupg' --no-default-keyring --keyring /Users/username/.gnupg/pubring.gpg --version
>>>
It is quite possible to manipulate the paths to use alternative keyrings or possibly relative paths for a portable implementation.
Anyway, here's a nice and quick demonstration with the keyrings (assuming the same config file as above):
import gnupg
from config import *
gpg = gnupg.GPG(gnupghome=gpg_home, keyring=pring, secret_keyring=sring)
gpg.encoding = "latin-1"
pkeys = gpg.list_keys(False)
skeys = gpg.list_keys(True)
pnum = len(pkeys)
snum = len(skeys)
print("""
%s contains %d public keys
%s contains %d private keys
""" % (pub_ring, pnum, sec_ring, snum))
I came across the same issue and found a solution to the problem. The problem is OSX specific and is being caused because the gpg binary itself is not found.
So, passing the full path to the binary as an argument solved the problem.
gpg = gnugp.GPG(gnupghome='/Users/myname/.gnupg', gpgbinary='/usr/local/bin/gpg')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With