title: Python pyinstaller windows exe with openssl
body: <p>I have for the last few days testing the ability to create a windows executable from a python script that requires pyOpenSSL.  I am getting an error that I am not sure how to solve or even a point of reference to solve.  I have some applications that I want to use pyOpenSSL, then compile into an windows exe.  I am doing this right now with a Chilkat library (www.chilkatsoft.com/) but now want to use other modules that need pyOpenSSL.</p>
  
  <p>The error that I get when I execute my single or directory compiled application is </p>
  
  <pre><code>IOError: [Errno 2] No such file or directory: 'H:\\Dev\\BS_DEV\\Win_Env\\ENV_Tests\\test_gen_cert\\build\\out00-PYZ.pyz\\__pycache__\\_Cryptography_cffi_f3e4673fx399b1113.c'
  </code></pre>
  
  <p>The test code that I am using will generate a public and private certificate, this runs ok when executed using the .py file.</p>
  
  <pre><code>from OpenSSL import crypto, SSL
  from socket import gethostname
  from pprint import pprint
  from time import gmtime, mktime
  from os.path import exists, join
  
  CERT_FILE = "myapp.crt"
  KEY_FILE = "myapp.key"
  
  def create_self_signed_cert():
      """
      If datacard.crt and datacard.key don't exist in cert_dir, create a new
      self-signed cert and keypair and write them into that directory.
      """
      cert_dir = '.'
      if not exists(join(cert_dir, CERT_FILE)) \
              or not exists(join(cert_dir, KEY_FILE)):
  
          # create a key pair
          k = crypto.PKey()
          k.generate_key(crypto.TYPE_RSA, 1024)
  
          # create a self-signed cert
          cert = crypto.X509()
          cert.get_subject().C = "US"
          cert.get_subject().ST = "Minnesota"
          cert.get_subject().L = "Minnetonka"
          cert.get_subject().O = "my company"
          cert.get_subject().OU = "my organization"
          cert.get_subject().CN = gethostname()
          cert.set_serial_number(1000)
          cert.gmtime_adj_notBefore(0)
          cert.gmtime_adj_notAfter(10*365*24*60*60)
          cert.set_issuer(cert.get_subject())
          cert.set_pubkey(k)
          cert.sign(k, 'sha1')
  
          open(join(cert_dir, CERT_FILE), "wt").write(
              crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
          open(join(cert_dir, KEY_FILE), "wt").write(
              crypto.dump_privatekey(crypto.FILETYPE_PEM, k))
  
  if __name__ == "__main__":
      create_self_signed_cert()
  </code></pre>
  
  <p>I have been using pyinstaller before and have compiled openssl programs with it on my OSX (Mac) box.</p>
  
  <p>Here is my spec file:</p>
  
  <pre><code># -*- mode: python -*-
  block_cipher = None
  
  a = Analysis(['scert.py'],
               pathex=['H:\\Dev\\BS_DEV\\Win_Env\\ENV_Tests\\test_gen_cert'],
               hiddenimports=[],
               hookspath=None,
               runtime_hooks=None,
               excludes=None,
               cipher=block_cipher)
  pyz = PYZ(a.pure,
               cipher=block_cipher)
  exe = EXE(pyz,
            a.scripts,
            a.binaries + [
                    ('ssleay32.dll', 'C:\\openssl-1.0.1i\\bin\\ssleay32.dll', 'BINARY'),
                    ('libeay32.dll', 'C:\\openssl-1.0.1i\\bin\\libeay32.dll', 'BINARY'),
                  ],
            a.zipfiles,
            a.datas,
            name='scert.exe',
            debug=False,
            strip=None,
            upx=True,
            console=True )
  </code></pre>
  
  <p>I have compiled my own version of OpenSSL and used pre compiled windows binaries.</p>
  
  <p>Here is some information on my environment:
  Windows 8 64 bit with 8 GiG of RAM
  Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32</p>
  
  <p>Any Ideas??</p>
  
  <p>Has anyone ever package a windows executable from a python script that requires OpenSSL?</p>
  
  <p>If this cannot be done, is there another package tool (opensource or purchase) that I can solve this problem?</p>
  
  <p>Thank you
  Tom</p>
  
  <p>OK, I think I might have found an answer, I got it working but need to verify this on another computer.
  I noticed that the error was coming from cffi wanting to compile something (not sure what it was) at the time of execution and/or not finding what cffi compiled.  So I looked around and was going to recompile cryptography and cffi.  Since I was on windows I then installed the free pyOpenSSL package from (www.egenix.com/products/python/pyOpenSSL/).  From that I was able to compile my script into an executable. </p>
  
  <p>I just compiled it into a directory and was able to see all the files, here is a list of what was in my dist directory:</p>
  
  <pre><code>OpenSSL.SSL.pyd
  OpenSSL.crypto.pyd
  OpenSSL.rand.pyd
  _ctypes.pyd
  _hashlib.pyd
  _socket.pyd
  _ssl.pyd
  bz2.pyd
  libeay32.dll
  microsoft.vc90.crt.manifest
  msvcm90.dll
  msvcp90.dll
  msvcr90.dll
  msvcrt.dll
  mx.DateTime.mxDateTime.mxDateTime.pyd
  mx.TextTools.mxTextTools.mxTextTools.pyd
  mx.Tools.mxTools.mxTools.pyd
  python27.dll
  pywintypes27.dll
  scert.exe
  scert.exe.manifest
  select.pyd
  ssleay32.dll
  unicodedata.pyd
  user32.dll
  win32api.pyd
  win32evtlog.pyd
  </code></pre>
  
  <p>I will be doing more testing tonight
  TJ</p>