简体   繁体   English

无法在山狮上导入twisted.internet.protocol模块

[英]Can't import twisted.internet.protocol module on mountain lion

I am trying to implement a TCP server and using python script. 我正在尝试实现TCP服务器并使用python脚本。 So I created a server.py on my desktop and imported the following modules: 所以我在桌面上创建了一个server.py并导入了以下模块:

from twisted.internet.protocol import Factory,Protocol

It's already failing at that point, because if I run: python server.py , I got the following output: 它已经失败了,因为如果我运行: python server.py ,我得到以下输出:

Traceback (most recent call last):
  File "server.py", line 4, in <module>
    from twisted.internet.protocol import Factory,Protocol
ImportError: No module named twisted.internet.protocol

I also use sys.path to print my Python directories and I got these outputs: 我也使用sys.path打印我的Python目录,我得到了这些输出:

['/Users/Alfred/Desktop',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
'/Library/Python/2.7/site-packages']

What could have gone wrong with my python setup? 我的python设置可能出了什么问题?

Is it because of the privilege of my account? 是因为我账户的特权吗? I logged in as the admin on this machine. 我在这台机器上以管理员身份登录。

The output of python -v -c 'import twisted.internet.protocol' might reveal the problem. python -v -c 'import twisted.internet.protocol'的输出可能会揭示问题。 As a first check, though, make sure you don't have a file named "twisted.py" sitting around. 但是,作为第一次检查,请确保您没有名为“twisted.py”的文件。

Based on your sys.path , you've installed Python from python.org, or compiled your own, rather than using the built-in Python that has Twisted pre-installed. 基于您的sys.path ,您已经从python.org安装了Python,或者编译了自己的Python,而不是使用预先安装了Twisted的内置Python。 If you install your own Python, you have to install your own version of Twisted into that version of Python. 如果您安装自己的Python,则必须在该版本的Python中安装自己的Twisted版本。 You can use pip or easy_install or download directly from the Twisted site and run setup.py . 您可以使用pipeasy_install或直接从Twisted站点下载并运行setup.py

type -p python in a shell will tell you which version you're using; 在shell中type -p python会告诉你正在使用哪个版本; I suspect it will be in /usr/local/bin/ . 我怀疑它将在/usr/local/bin/

/usr/bin/python2.7 -c 'from twisted.internet.protocol import Factory,Protocol' ought to succeed. /usr/bin/python2.7 -c 'from twisted.internet.protocol import Factory,Protocol'应该成功。 If not, then I don't know what you've done to your sytem Python installation :). 如果没有,那么我不知道你对sytem Python安装做了什么:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM