Can't import shared.SharedService in thrift tutorial

Question:

I seem to be mis-understanding something about Apache Thrift. I have it installed and generated python bindings using the tutorial.thrift file. I manipulated my PYTHONPATH environmental variable to allow me to import from the generated files. When I import tutorial.Calculator' it can't findshared.SharedService`. Is this a dependency of Apache Thrift or something I have to define?

ericu@eric-phenom-linux:~/tmp$ export PYTHONPATH=$PYTHON:`pwd`/gen-py
ericu@eric-phenom-linux:~/tmp$ echo $PYTHONPATH
:/home/ericu/tmp/gen-py
ericu@eric-phenom-linux:~/tmp$ c
c: command not found
ericu@eric-phenom-linux:~/tmp$ cd 
ericu@eric-phenom-linux:~$ pyhon
No command 'pyhon' found, did you mean:
 Command 'python' from package 'python-minimal' (main)
pyhon: command not found
ericu@eric-phenom-linux:~$ python
Python 2.7.5 Stackless 3.1b3 060516 (default, Sep 23 2013, 20:17:03) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tutorial
>>> tutorial.Calculator
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Calculator'
>>> import tutorial.Calculator
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ericu/tmp/gen-py/tutorial/Calculator.py", line 10, in <module>
    import shared.SharedService
ImportError: No module named shared.SharedService
>>> 
Asked By: Eric Urban

||

Answers:

The SharedService is part of another Thrift IDL file, which is included into the tutorial.thrift IDL file. Tbus, you have to generate the code for the Shared service as well. The easiest way is to call the Thrift compiler with the -r option (“recursive”) which will generate code for the passed IDL file and all included dependencies.

The tutorial pages are indeed misleading here. If you want to file a JIRA ticket, please do.

Answered By: JensG

please add https://raw.githubusercontent.com/apache/thrift/master/tutorial/shared.thrift to your thrift directory at first ,then thrift -r -gen

Answered By: Knight.zhou
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.