9

I'm pretty new to python and twisted and I tried to run a simple twisted script but failed.

My environment:

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

The script called test.py:

from twisted import reactor
reactor.run()

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

Then I opened eclipse and created a new PyDev project then adding a py file called test.py and typed the code above in.

When I tried to run 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 the PYTHONPATH and added the "twisted source folder" in the external libraries tab but the errors was still there.(nothing changed)

Hope someone can help me out here, thanks in advance :)

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
supersuraccoon
  • 1,621
  • 4
  • 20
  • 36

5 Answers5

4

Several things to try here -

  1. Since your env is Mac OSX. I installed commandline tools from Xcode which solved this problem. Xcode 4.4 and later install Command Line Tools

  2. For the sake of completeness. If this on Ubuntu, then apt-get install python-twisted generally works.

  3. from your eclipse put this in your python script -

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

    This tells all the paths that python looks for when you import something. If you don't find twisted path there then add it to this like so - sys.path.append(twisted_dir_path)

  4. Finally, if all the above does not help. type -p python in a shell will tell you which version you're using; I would mostly be /usr/bin/ or some variant of it.

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

Community
  • 1
  • 1
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
3

This has happened to me so many times, but I figured out previously. Basically, if you have too many versions of python on your mac, Aptana Studio for some reason gets confused, so make sure that your twisted installation is using the same python as the command line (even though you already added twisted to Aptana). I'll get into more specifics later

Ok, I think this is how you do it:

  1. Grab the python path of the python that has twisted installed correctly, using this
    import sys
    print sys.path
    And just grab the base path, nothing to specific.
  2. Go to Aptana-preferences-PyDev-Interpreter-Python
  3. And then just add a new interpreter (in my case I just called it python1 and set is the first one. If that doesn't work then just set a new PYTHONPATH right below it.
  • Hi, I do have too many python installed. that's why I got an error with segmentation fault 11. any workaround? – OMGPOP Aug 17 '13 at 02:47
  • @OMGPOP This was a while ago but I let me try to describe the solution in my answer. Give me a couple of hours, I am not at my computer now. –  Aug 17 '13 at 18:27
  • Segmentation fault: 11 – OMGPOP Aug 18 '13 at 06:58
3

You need to install incremental before twisted

pip install --upgrade incremental
pip install Twisted

This should solve the problem.

1

Not sure how you installed twisted, did you try easy_install or pip, or install in manually?

If you want to make sure it's installed properly, try opening up a terminal, typing python, and then "import twisted". If it comes back without errors, then it's installed. You might need to set the PYTHONPATH variable to include the source folder.

Then, since you're using pydev in eclipse, you need to refresh your configuration each time you add a new library.

On the mac, this is under preferences -> PyDev -> Interperter-Python

I find it works best to remove my configuration and re-add it, for it to pick up everything. But you can click on new folder (select the folder with the init.py), or the new egg, if it's an easy_install egg (a .egg file is a zip file, if it's unzipped, you'll see a EGG-INFO folder in the subdirectory of the folder you want to select).

Adam Morris
  • 8,265
  • 12
  • 45
  • 68
1

You should set the correct PYTHONPATH in Eclipse as following:

  1. right click your project.
  2. select Properties
  3. select pyDev-PYTHONPATH
  4. add your twised in the external libraries
ray_linn
  • 1,382
  • 10
  • 14