Undefined variable from import when using protocol buffers in PyDev

I have a PyDev project that uses protocol buffers. Protocol buffer files are located in the zip file generated by the protoc compiler. Everything works when I run the program, however PyDev reports "Undefined variable from import" for each enum constant. For example:

import model_pb2 value = model_pb2.Expression(type = model_pb2.Expression.PARAMETER) 

It reports the enum constant "PARAMETER" as an undefined variable. There are dozens of such errors in my program, and I would fix them “correctly” (i.e. I didn’t just suppress the warning.)

+6
source share
3 answers

I found that using for built-in functions can work, but only if all proto files are in a separate part located in an external library (see http://pydev.org/manual_101_project_conf2.html ).

This should work:

  • Move (or unzip) compiled proto files, including model_pb2.py, to a directory outside of the pydev project.
  • Add the empty __init__.py file to the same directory as model_pb2.py to make sure it can be imported as a library.
  • In eclipse, go to Windows -> Settings -> pydev -> Interpreter
  • Add the directory with model_pb2.py to the Libraries.
  • Add model_pb2 to forced buildins.

If you are not dependent on autocompletion, you can use ctrl + 1 to ignore these errors instead as described in this answer . This has been tested with Eclipse Kepler and pydev 2.8.

+1
source

Have you tried adding "model_pb2" to your forced built-in? See http://pydev.org/manual_101_project_conf2.html for more details.

0
source

I ran into this problem with protobuf 2.6.1 and PyDev 4.5.5. I tried the suggestions above, and none of them helped me in this matter. What ultimately got rid of the undefined variable errors when using protobuf enums was simple:

Access to enumeration on the protobuf object instance, and not on the protobuf module.

I'm not sure if this can be applied to the case of using OP, but in my opinion it was as simple as:

 from myprotobuf_module import SomeProtobufMessage some_protobuf_object = SomeProtobufMessage() some_enum = some_protobuf_object.SOME_ENUM 
0
source

Source: https://habr.com/ru/post/919403/


All Articles