TypeError: Can't convert 'GenKey' object to str implicitly

63 views
Skip to first unread message

slowb...@gmail.com

unread,
Oct 6, 2018, 3:50:55 PM10/6/18
to python-gnupg
Heads up, I was using this Salty Crane tutorial for code on my Raspberry Pi:
https://www.saltycrane.com/blog/2011/10/python-gnupg-gpg-example/

Got the following Traceback:
(Pi) root@raspberrypi:/Pi# ./test.py
Traceback (most recent call last):
 
File "./test.py", line 15, in <module>
    ascii_armored_public_keys
= gpg.export_keys(key)
 
File "/Pi/lib/python3.5/site-packages/gnupg.py", line 1222, in export_keys
    p
= self._open_subprocess(args)
 
File "/Pi/lib/python3.5/site-packages/gnupg.py", line 886, in _open_subprocess
    startupinfo
=si)
 
File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
    restore_signals
, start_new_session)
 
File "/usr/lib/python3.5/subprocess.py", line 1221, in _execute_child
    restore_signals
, start_new_session, preexec_fn)
TypeError: Can't convert 'GenKey' object to str implicitly


(Pi) root@raspberrypi:/Pi#

This was my code:
#!/usr/bin/env python
import os

import gnupg


gpg
= gnupg.GPG(gnupghome='/home/pi')


input_data
= gpg.gen_key_input(
    name_email
='pi@localhost',
    passphrase
='XXXXXXXXXXXX')


key
= gpg.gen_key(input_data)
ascii_armored_public_keys
= gpg.export_keys(key)

ascii_armored_private_keys
= gpg.export_keys(key, True)


with open('os_command.pub.key', 'w') as f:
    f
.write(ascii_armored_public_keys)


with open('os_command.priv.key', 'w') as f:
    f
.write(ascii_armored_private_keys)


with open('os_command.key', 'w') as f:
    f
.write(ascii_armored_public_keys)
    f
.write(ascii_armored_private_keys)

The solution was to wrap an str() around this line:
key = str(gpg.gen_key(input_data))


I am posting this here because I did not find this problem with Google. I'm sure someone else may encounter this error, so I'm showing it for posterity :-) I don't see how to post on the Salty Crane tutorial page or I would.

I also did not see this explained in the documentation. Maybe I just missed it.

Additionally, I also had to change the export_keys function calls:
ascii_armored_public_keys = gpg.export_keys(key, passphrase=passphrase)
ascii_armored_private_keys
= gpg.export_keys(key, True, passphrase=passphrase)

The error was, "ValueError: For GnuPG >= 2.1, exporting secret keys needs a passphrase to be provided"
Reply all
Reply to author
Forward
0 new messages