Search code examples
pythondependenciestwisted

Python twisted ImportError: cannot import name unicode


Something wrong with my twisted dependencies, it worked and now i`m getting this error while importing protocol and reactor:

Python 2.7.8 (default, Nov 25 2014, 09:23:41)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from twisted.internet import reactor, protocol
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nick/env/env/lib/python2.7/site-packages/twisted/__init__.py", line 56, in <module>
    from twisted.python import compat
  File "/home/nick/env/env/lib/python2.7/site-packages/twisted/python/compat.py", line 23, in <module>
    import sys, string, socket, struct
  File "socket.py", line 1, in <module>
    from twisted.internet.protocol import Protocol, Factory
  File "/home/nick/env/env/lib/python2.7/site-packages/twisted/internet/protocol.py", line 17, in <module>
    from twisted.python import log, failure, components
  File "/home/nick/env/env/lib/python2.7/site-packages/twisted/python/log.py", line 19, in <module>
    from twisted.python.compat import unicode, _PY3
ImportError: cannot import name unicode

Solution

  • It`s how Python searching and including libs. In my directory was file called socket.py In my script i included "from twisted.internet import reactor, protocol" Twisted lib loads socket lib - so my socket.py overheads standart lib. So, be carefully with naming scripts...

    Also if you load python console, current directory includes as lib searching path.