1

When running a Twisted (OS X 10.9.3) file via twistd -n -y chatserver.py it fails with error:

...line 40, in <module>
    from OpenSSL.SSL import Error, ZeroReturnError, WantReadError
exceptions.ImportError: No module named OpenSSL.SSL

Failed to load application: No module named OpenSSL.SSL

Twisted and ssl import fine in a python shell:

python
Python 2.7.3 (default, Oct 26 2012, 16:12:44) 
[GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.60))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import twisted
>>> import ssl
>>> exit()

Any pointers in the right direction?

Thanks!

Adam Ashwal
  • 1,472
  • 1
  • 19
  • 36
  • You haven't provided enough information to diagnose the problem. You've probably installed some alternate Python or done something weird though; pyOpenSSL is pre-installed on OS X. What Python are you running, are you using virtualenv, are you using pip, or easy_install, etc? – Glyph Jun 13 '14 at 07:29
  • Specifically on 10.9.3 if I import OpenSSL.SSL and [print the path](http://stackoverflow.com/a/248862/3334178) I get: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/OpenSSL/SSL.so If your running a modern version of OS X (and not running a virtualenv/brew/port/fink) I suspect you've corrupted the builtin python packages. – Mike Lutz Jun 13 '14 at 14:42

1 Answers1

7

By the information provided, I would assume you either:

  • don't have pyOpenSSL loaded (the error said it was trying to import "OpenSSL.SSL", you tried importing "ssl", I don't know if "ssl" maps to pyOpenSSL). See twisted's TLS docs.

or

  • your twistd is running a different instance of python then what you get at the terminal prompt

Update:

Given the feedback in the comments, and following the info from SO: Retrieving python module path and Find full path of python interpreter

Try running the following to see your current python paths:

For twistd put the following in a file and run it the same way your currently running twistd:

from twisted.application.service import Application
from twisted.internet import reactor
import sys

def print_path():
    print "   ----    The path to the twistd python is: " + str(sys.executable) + "   ----"
    reactor.stop()

application = Application("path_test")
reactor.callWhenRunning(print_path)

For your command line python just run the following in interactive mode:

import sys
print sys.executable

In my case (running OS X 10.9.3, with a somewhat customized python I get)

twistd:

% twistd -n -y twisted-question-24191967.py
2014-06-13 11:08:19-0400 [-] Log opened.
2014-06-13 11:08:19-0400 [-] twistd 13.2.0 (/usr/bin/python 2.7.5) starting up.
2014-06-13 11:08:19-0400 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2014-06-13 11:08:19-0400 [-]    ----    The path to the twistd python is: /usr/bin/python   ----
2014-06-13 11:08:19-0400 [-] Main loop terminated.
2014-06-13 11:08:19-0400 [-] Server Shut Down.

Interactively:

>>> import OpenSSL.SSL
>>> print OpenSSL.SSL.__file__
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/OpenSSL/SSL.so
>>> import sys
>>> print sys.executable
/usr/bin/python
Community
  • 1
  • 1
Mike Lutz
  • 1,812
  • 1
  • 10
  • 17
  • I get a `Requirement already satisfied` when trying to install pyopenssl via pip but `from OpenSSL import SSL` still throws an error: `No module named OpenSSL` – Adam Ashwal Jun 12 '14 at 21:33
  • 2
    So quite probably the latter explanation. Just for the record, `ssl` and pyOpenSSL/`OpenSSL` are entirely separate things. – Jean-Paul Calderone Jun 12 '14 at 22:31