pythonconfigurationtorstem

"ImportError: cannot import name connect" when trying to import Stem


I am trying to go through Stem's tutorial for Python, using Ubuntu 14.04. After using sudo apt-get install python-stem and installing Stem, I found myself not knowing how to use it.

I tried to run simple code below, which I got from https://stem.torproject.org/api/connection.html#stem.connection.connect

import sys

from stem.connection import connect

if __name__ == '__main__':
  controller = connect()

  if not controller:
    sys.exit(1)  # unable to get a connection

  print 'Tor is running version %s' % controller.get_version()
  controller.close()

but I get this error:

~/example$ python controller.py
Traceback (most recent call last):
  File "controller.py", line 3, in <module>
    from stem.connection import connect
ImportError: cannot import name connect

Seems like Python cannot find the Stem code.
My question is how to fix this? Do I need to do something with installed Stem file?


Solution

  • I was able to get it running by using:

    sudo apt-get install python-pip
    sudo pip install stem
    

    Pip is the Python package manager which will install the modules to the correct paths for Python.