ImportError: no module named twisted

I am new to python and twisted, and I tried to run a simple twisted script, but could not.

My environment:

MacOX 10.7.2 + Python 2.7.1 + Twisted 11.0.0 + eclipse + PyDev

the script is called test.py:

from twisted import reactor reactor.run() 

I tried to run it in the terminal and everything is working fine.

Then I opened eclipse and created a new PyDev project, and then added a py file called test.py and enter the code above.

When I tried to start it, I got errors like:

 Traceback (most recent call last): File "/Users/user/Documents/workspace/TwistedDemo/test.py", line 2, in <module> from twisted import reactor ImportError: No module named twisted 

Then I checked PYTHONPATH and added the "inverted source folder" in the external libraries tab, but there were still errors. (nothing changed)

Hope someone can help me, thanks in advance :)

+8
python pydev
source share
4 answers

A few things to try here -

  • Since your env is Mac OSX. I installed the command line tools from Xcode that solved this problem. Xcode 4.4 and later install command line tools

  • For the sake of completeness. If it is on Ubuntu, then apt-get install python-twisted usually works.

  • from your eclipse put this in your python script -

    python -c 'import sys; print sys.path '

    This talks about all the paths python is looking for when you import something . If you don’t find the right path, add it like this: sys.path.append(twisted_dir_path)

  • Finally, if all of the above does not help. type -p python in the shell will tell you which version you are using; I would basically be /usr/bin/ or its variant.

    Then /usr/bin/python2.7 -c 'from twisted.internet.protocol import Factory,Protocol' should /usr/bin/python2.7 -c 'from twisted.internet.protocol import Factory,Protocol' .

+4
source share

This has happened to me so many times, but I understood it before. Basically, if you have too many versions of python on your Mac, Aptana Studio is somehow confused, so make sure your twisted installation uses the same python as on the command line (even if you already added twisted Aptana). I will tell you more later.

OK, I think this is how you do it:

  • Grab the python path for the python that correctly twisted using this
    import sys
    print sys.path
    And just take the base path, nothing concrete.
  • Go to Aptana-preferences-PyDev-Interpreter-Python
  • And then just add a new interpreter (in my case, I just called it python1 , and set is the first. If that doesn't work, just install the new PYTHONPATH right under it.
+3
source share

Not sure how you installed twisted, tried easy_install or pip, or installed manually?

If you want to make sure that it is installed correctly, try opening a terminal by typing python and then "import twisted". If it returns without errors, it is installed. You may need to set the PYTHONPATH variable to include the source folder.

Then, since you are using pydev in eclipse, you need to update your configuration every time you add a new library.

On a poppy, it depends on preferences -> PyDev -> Interperter-Python

I find it best suited for deleting my configuration and re-adding it so that it takes everything away. But you can click on a new folder (select the folder with init .py) or a new egg if it is an easy_install egg (the .egg file is a zip file, if it is unpacked, you will see the EGG folder in the subdirectory folder that you want to select -INFO).

+1
source share

You must set the correct PYTHONPATH in Eclipse as follows:

  • right click your project.
  • select Properties
  • select pyDev-PYTHONPATH
  • add your tweets to external libraries
+1
source share

All Articles