简体   繁体   English

在python3中,安装twis​​ted后,当导入twisted时,它显示“ ImportError:没有名为“ twisted”的模块”

[英]In python3 ,after install twisteed, when import twisted it shows“ImportError: No module named 'twisted' ”

I install the ubuntu 14.04LTS as a virtual machine, it has already install the python2.7 et python3.4 in the machine. 我将ubuntu 14.04LTS安装为虚拟机,它已经在计算机中安装了python2.7和python3.4。 After that I use the command sudo apt-get install python-twisted to install the framework twisted, I can use import twisted in python 2.7 but I can't use this same command import twisted in python 3.4. 从那以后,我用命令sudo apt-get install python-twisted安装框架扭曲,我可以使用import twisted在Python 2.7,但我不能使用这个相同的命令import twisted在Python 3.4。 It shows 表明

>>> import twisted
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'twisted'

I have try many way to install the twisted, like 我尝试了很多方法来安装双绞线,例如

sudo apt-get install python-setuptools
Sudo apt-get install python-dev
Sudo easy_install twisted

or 要么

sudo pip install twisted

even download the pack tar xjf Twisted-13.1.0.tar.bz2 and tar xzf zope.interface-3.6.1.tar.gz than run python setup.py install 甚至下载tar xjf Twisted-13.1.0.tar.bz2tar xzf zope.interface-3.6.1.tar.gz比运行python setup.py install

However,unfortunately, these don't work. 但是,不幸的是,这些无效。 Does anybody knows how to solute this problem? 有人知道如何解决这个问题吗? Thanks ! 谢谢 !

This is because all of these libraries are installing to Python 2.7's libraries. 这是因为所有这些库都安装在Python 2.7的库中。

To install to Python 3, use pip3 , not pip : 要安装到Python 3,请使用pip3 ,而不要使用pip

sudo pip3 install twisted

To understand why this happened to you, note that Ubuntu symlinks python to the python2.7 executable and python3 to python3.4 . 要了解为什么发生这种情况,请注意Ubuntu将python链接到python2.7可执行文件,并将python3链接到python3.4

From what I understand, packages such as these on apt-get internally call python during installation rather than python3 . 据我了解,诸如apt-get上的软件包在安装过程中apt-get内部调用python ,而不是python3 Hence why nothing gets installed to Python 3.4 in those cases. 因此,在这种情况下为什么没有安装任何东西到Python 3.4。

The safe alternative is always to use pip for Python installation. 安全的选择始终是使用pip进行Python安装。 pip3 is symlinked to the version of pip that services only Python 3.4, so always call that for Python 3 packages. pip3与仅支持Python 3.4的pip版本pip3链接,因此请始终为Python 3软件包调用它。

An even better solution is to use virtualenv to isolate different Python and pip versions from each other. 更好的解决方案是使用virtualenv将不同的Python和pip版本彼此隔离。 That way, when you enter a virtual environment, you can use pip without worrying about the symlinked version. 这样,当您进入虚拟环境时,可以使用pip而不用担心符号链接版本。

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

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