Python problem, grpc imports not working

2,815 views
Skip to first unread message

ccl...@dieselpoint.com

unread,
Dec 13, 2016, 12:51:40 PM12/13/16
to grpc.io
I'm a Python newbie, perhaps this is just my misunderstanding of Python...

I'm getting import errors. I generated some _pb2 files from my .proto file using this:

python -m grpc.tools.protoc -I/path/to/proto/dir/proto --python_out=. --grpc_python_out=. /path/to/proto/dir/proto/myprotofile.proto

It created myprotofile_pb2.py and myprotofile_pb2_grpc.py and put them in my Python package directory.

When I open myprotofile_pb2.py in Eclipse/PyDev, it gives me the error "Unresolved import: _descriptor" on this line:

from google.protobuf import descriptor as _descriptor

and when I try to run my own code that imports it using "from . import dieseldb_pb2", I get "SystemError: Parent module '' not loaded, cannot perform relative import"

Running "pip install protobuf" generates "Requirement already satisfied", so that's not the problem.

What am I doing wrong?

(I'm using the latest version of everything. Python 3.5.2.)

Masood Malekghassemi

unread,
Dec 13, 2016, 1:08:45 PM12/13/16
to ccl...@dieselpoint.com, grpc.io
Alrighty, here's a deluge of question marks:

What OS distribution (and version of that distribution) are you using?
Is your `pip` command for Python 2 or for Python 3?
Is the 'Requirement already satisfied' line accompanied by any statements about the OS owning a package (s.t. installation of true newer versions are ignored because Python's package ecosystem has multiple distribution mechanisms)?
If you run `$YOUR_PYTHON3_COMMAND_HERE -c 'import google.protobuf.descriptor'` what's the output? $YOUR_PYTHON3_COMMAND_HERE may just be e.g. `python3`.
If you open an interpreter in the directory of the dieseldb_pb2 and run `import dieseldb_pb2`, what's the entire output?
Have you considered the comments on this SO question: http://stackoverflow.com/questions/16981921/relative-imports-in-python-3 ?

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/1678e5b2-378f-48b8-94d1-aa37f9bccfa8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nathaniel Manista

unread,
Dec 13, 2016, 1:10:32 PM12/13/16
to ccl...@dieselpoint.com, grpc.io
On Tue, Dec 13, 2016 at 9:51 AM, <ccl...@dieselpoint.com> wrote:
Nothing obvious is springing to mind.

What's the output of "pip freeze"?

Are you working with a virtual environment, the Python environment on your system (what is your system?), or some other Python environment?

... and all of Masood's questions. :-P
-Nathaniel

ccl...@dieselpoint.com

unread,
Dec 13, 2016, 1:42:16 PM12/13/16
to grpc.io, ccl...@dieselpoint.com
Let me do the best I can:

1. OS distribution: Windows 10

2. pip --version:
pip 9.0.1 from c:\users\ccleve\appdata\local\programs\python\python35\lib\site-packages (python 3.5)

3. Is the 'Requirement already satisfied' line accompanied by any statements about the OS owning a package? No. Here's the output:
Requirement already satisfied: protobuf in c:\users\ccleve\appdata\local\programs\python\python35\lib\site-packages

4. python -c "import google.protobuf.descriptor" generates no output. It just returns.

5. python -c "import dieseldb_pb2" generates no output. It just returns.

6. python example.py, where example contains "from . import dieseldb_pb2" generates:
Traceback (most recent call last):
  File "example.py", line 10, in <module>
    from . import dieseldb_pb2

SystemError: Parent module '' not loaded, cannot perform relative import

7. python example.py, when it contains just "import dieseldb_pb2":
Weirdly, it seems to work. It generates errors later in the program, which indicates it's getting past the import line. Eclipse/PyDev still says the import can't be found in the IDE editor.

8. I'll try to understand that SO question. Again, my Python understanding is weak.

9. pip freeze:
Django==1.10.4
enum34==1.1.6
grpcio==1.0.2
grpcio-tools==1.0.2
protobuf==3.1.0.post1
six==1.10.0
virtualenv==15.1.0
virtualenvwrapper-win==1.2.1

10. Not using the virtual environment.

Maybe this is all just an Eclipse problem.

ccl...@dieselpoint.com

unread,
Dec 13, 2016, 2:26:53 PM12/13/16
to grpc.io, ccl...@dieselpoint.com
I have a partial solution. In Eclipse, I added a new source folder under Preferences/PyDev/Interpreters/Python Interpreter/Libraries. That forced some kind of rebuild and the problem went away in my example.py. The local import was resolved.

The problem still remains in dieseldb_pb2.py and dieseldb_pb2_grpc.py. Imports still show up as errors in the IDE.
Reply all
Reply to author
Forward
0 new messages