在OS X上,Twisted应用程序失败,错误为“无法加载应用程序:没有名为OpenSSL.SSL的模块”

1 投票
1 回答
4637 浏览
提问于 2025-04-18 09:35

在OS X 10.9.3上运行一个Twisted文件,使用命令twistd -n -y chatserver.py时出现错误:

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

在Python环境中,Twisted和ssl都能正常导入:

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

有没有什么建议可以帮忙解决这个问题?

谢谢!

1 个回答

7

根据提供的信息,我猜测你可能遇到以下两种情况:

  • 你没有加载pyOpenSSL(错误信息显示它试图导入“OpenSSL.SSL”,而你尝试导入的是“ssl”,我不确定“ssl”是否对应于pyOpenSSL)。可以查看twisted的TLS文档

或者

  • 你的twistd正在运行一个与终端提示符下不同的Python实例。

更新:

根据评论中的反馈,以及参考了Stack Overflow的信息:获取Python模块路径查找Python解释器的完整路径

尝试运行以下命令来查看你当前的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:

% 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

撰写回答