Skip to main content
 首页 » 编程设计

python之Twisted 应用程序在 OS X 上失败并出现错误 "Failed to load application: No module named OpenSSL.SSL"

2025年05月04日28sky-heaven

当通过 twistd -n -y chatserver.py 运行 Twisted (OS X 10.9.3) 文件时失败并出现错误:

...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 和 ssl 在 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() 

有没有正确方向的指示?

谢谢!

请您参考如下方法:

根据所提供的信息,我假设您:

  • 没有加载 pyOpenSSL(错误说它正在尝试导入“OpenSSL.SSL”,你尝试导入“ssl”,我不知道“ssl”是否映射到 pyOpenSSL)。参见 twisted's TLS docs .

或

  • 你的 twistd 运行的是与你在终端提示符下得到的不同的 python 实例

更新:

鉴于评论中的反馈,并遵循来自 SO 的信息:Retrieving python module path和 Find full path of python interpreter

尝试运行以下命令以查看您当前的 python 路径:

对于 twistd,将以下内容放入一个文件中,并以与当前运行的 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) 

对于您的命令行 python,只需在交互模式下运行以下命令:

import sys 
print sys.executable 

在我的例子中(运行 OS X 10.9.3,我得到了一些自定义的 python)

扭曲:

% 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. 

交互方式:

>>> 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