Adam Ashwal
Adam Ashwal

Reputation: 1472

Twisted app fails with error "Failed to load application: No module named OpenSSL.SSL" on OS X

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!

Upvotes: 1

Views: 4655

Answers (1)

Mike Lutz
Mike Lutz

Reputation: 1832

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

Upvotes: 7

Related Questions