ImportError: No module named twisted.internet

北城余情 提交于 2019-11-30 08:32:27
Milev

If you use pip just try:

pip install twisted

The same works with w3lib and lxml.

On some *nix systems this might give you a permission error. If that happens, try:

sudo -H pip install twisted

I figured out why this error was happening. For some reason, using apt-get to install a python package was not installing it right.

So, I had to download a tar ball and install the package from them.

I downloaded Twisted tar from here.

I did a tar xjf Twisted-13.1.0.tar.bz2 - this created a directory called Twisted-13.1.0

next, cd Twisted-13.1.0 Finally, python setup.py install

This gave me an error. Twisted required another package called zope.interface. So, I downloaded tar ball for zope.interface from here. Then, ran this command tar xzf zope.interface-3.6.1.tar.gz That created a folder called zope.interface-3.6.1. So, cd into zope.interface-3.6.1 and run python setup.py install

Note: Depending on your user's rights, you may want to do these commands in sudo mode. Just add the keyword sudo before every command.

please rename the file twisted.py to something else. whenever you import a function from a file the interpreter will search for the file in the current location and then it searches in the library. so if you have any file in the name of "twisted.py" you should probably rename it.

after renaming it. dont fail to remove the twisted.pyc file before running it again.

It happened to me too. Finally I figure out that there is a file named twisted.py my present working directory. I removed twisted.py and twisted.pyc. Problem resolved.

I figured out why apt-get install python-twisted was not enough or "installing it right", as you said, user1700184.

I use Debian Wheezy and Python 2.7.

I just had to move the folder named "twisted" from /usr/lib/python2.7/dist-packages/ to /usr/lib/python2.7/

The same has to be done with the package "zope" and any other one that you do install but is not retrieved when you try run your code.

However, why this is even necessary in my case is still a mystery since my sys.path does include both /usr/lib/python2.7/ and /usr/lib/python2.7/dist-packages, so whatever was under dist-packages should have been retrieved by the interpreter.

I think it is worth noting that if you use sudo to launch python you are using your original default system python. This is NOT the python that your PATH points to. For example if you are using Anaconda and you have updated your path such that which python points to path/to/anaconda/bin/python, sudo which python will still point to usr/bin/python.

So obviously sudo python twistedTester.py will not find the twisted module. To get around this you should explicitly pass the path to the anaconda python. Like so:

sudo path/to/anaconda/bin/python twistedTester.py
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!