setting gpg binary path on Windows

1,890 views
Skip to first unread message

David Crisp

unread,
May 25, 2014, 8:27:52 PM5/25/14
to python...@googlegroups.com
Hello,

Forgive me if this has been answered before, a  search did not find the answer to my problem:

I have the following:
Windows 7
gnupg 0.3.6
Gpg4win 2.2.1  (installed to default location of C:\Program Files(x86)\GNU\GnuPG\pub)

My problem is right at the initial stages of using the code as I am invoking gnupg for the first time using linux.

My Code:

import gnupg
pathtobin = "C:\\Program Files (x86)\\GNU\\GnuPG\\pub\\"
gpg = gnupg.GPG(gpgbinary = pathtobin, verbose = 'True' )

when I run that I get the following Error:

C:\Program Files (x86)\GNU\GnuPG\pub\ --status-fd 2 --no-tty --version
Traceback (most recent call last):
  File "C:/Users/dcrisp.RAPIDMAP/Documents/Python/origin/gpgdecrypt.py", line 7, in <module>
    gpg = gnupg.GPG(gpgbinary = pathtobin, verbose = 'True' )
  File "C:\Python33\lib\site-packages\gnupg.py", line 669, in __init__
    result.stderr))
ValueError: Error invoking gpg: 1: 'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

It appears that somewhere in the gppbinary string handling component the string is getting truncated to the first space.
This is awkward when trying to use the system with a default iinstall of gpg.

How do other people resolve this problem, because im SURE its been solved already and I am just missing something.

Regards,
David
 







Vinay Sajip

unread,
May 26, 2014, 6:06:35 AM5/26/14
to python...@googlegroups.com

On Monday, May 26, 2014 1:27:52 AM UTC+1, David Crisp wrote:
My problem is right at the initial stages of using the code as I am invoking gnupg for the first time using linux.
 
Using linux? You appear to be on Windows. What do you mean?

My Code:

import gnupg
pathtobin = "C:\\Program Files (x86)\\GNU\\GnuPG\\pub\\"
gpg = gnupg.GPG(gpgbinary = pathtobin, verbose = 'True' )

You need to specify the path to the executable, not the directory that contains it.
 
when I run that I get the following Error:

C:\Program Files (x86)\GNU\GnuPG\pub\ --status-fd 2 --no-tty --version
Traceback (most recent call last):
  File "C:/Users/dcrisp.RAPIDMAP/Documents/Python/origin/gpgdecrypt.py", line 7, in <module>
    gpg = gnupg.GPG(gpgbinary = pathtobin, verbose = 'True' )
  File "C:\Python33\lib\site-packages\gnupg.py", line 669, in __init__
    result.stderr))
ValueError: Error invoking gpg: 1: 'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

It appears that somewhere in the gppbinary string handling component the string is getting truncated to the first space.
This is awkward when trying to use the system with a default iinstall of gpg.

How do other people resolve this problem, because im SURE its been solved already and I am just missing something.

There may be a bug relating to spaces - I'll look into it - but you need to use the correct path to the executable - let us know how that goes.
The path (with spaces) is just passed to the subprocess module, which AFAIK deals with spaces.

Regards,

Vinay Sajip

Mitch Trachtenberg

unread,
Sep 5, 2014, 12:44:05 PM9/5/14
to python...@googlegroups.com
I've run into a similar problem.  I find that a test program fails as follows -- when I leave an unescaped space in "Program Files", the generated command contains an unescaped slash and the execution fails:
DEBUG:gnupg:['C:\\Program Files\\doesnotmatter', '--status-fd', '2', '--no-tty', '--version']
And when I escape the space with a slash, the slash is doubled rather than protecting the space that follows:
DEBUG:gnupg:['C:\\Program\\ Files\\doesnotmatter', '--status-fd', '2', '--no-tty', '--version']

The test program is as follows:

import pdb
import os.path
import gnupg
import logging

if __name__ == "__main__":
  logger = logging.basicConfig(filename="C:\\Users\\mitch\\logfile.txt",
                               level=logging.DEBUG)
  logging.info("Space between Program and Files")
  gpgbin = os.path.join("C:","\\Program Files","doesnotmatter")
  try:
   gpg = gnupg.GPG(gpgbinary=gpgbin)
  except:
    pass
  logging.info("Slash escaped space between Program and Files")
  gpgbin = os.path.join("C:","\\Program\ Files","doesnotmatter")
  try:
    gpg = gnupg.GPG(gpgbinary=gpgbin)
  except:
    pass

and the complete logged output is:
INFO:root:Space between Program and Files
DEBUG:gnupg:['C:\\Program Files\\doesnotmatter', '--status-fd', '2', '--no-tty', '--version']
DEBUG:gnupg:stderr reader: <Thread(Thread-1, initial daemon)>
DEBUG:gnupg:stdout reader: <Thread(Thread-2, initial daemon)>
DEBUG:gnupg:'C:\Program' is not recognized as an internal or external command,
DEBUG:gnupg:operable program or batch file.
INFO:root:Slash escaped space between Program and Files
DEBUG:gnupg:['C:\\Program\\ Files\\doesnotmatter', '--status-fd', '2', '--no-tty', '--version']
DEBUG:gnupg:stderr reader: <Thread(Thread-3, initial daemon)>
DEBUG:gnupg:stdout reader: <Thread(Thread-4, initial daemon)>
DEBUG:gnupg:'C:\Program\' is not recognized as an internal or external command,
DEBUG:gnupg:operable program or batch file.


 







Mitch Trachtenberg

unread,
Sep 5, 2014, 1:22:57 PM9/5/14
to python...@googlegroups.com
I also had difficulty providing a path to the binary.  I've found that embedding the complete path generated by os.path.join in literal double quotes works around the problem.
 
  gpgbin = os.path.join("C:","\\Program Files (x86)","GNU","GnuPG","gpg2.exe")
  gpgbin = "\"%s\"" % (gpgbin,) #embed space-containing path within literal double quotes
  gpg = gnupg.GPG(gpgbinary=gpgbin)
  public_keys = gpg.list_keys()
  print public_keys

 
Reply all
Reply to author
Forward
0 new messages