Undefined variable from import when using protocol buffers in PyDev

2024/9/22 23:20:34

I've got a PyDev project that uses protocol buffers. The protocol buffer files are located in a zip file generated by the protoc compiler. Everything works when I run the program, however PyDev reports "Undefined variable from import" for every enumeration constant. So for example:

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

It reports the enum constant "PARAMETER" as being an undefined variable. There are several dozen similar errors in my program, and I'd like to fix them "properly" (i.e. not simply suppressing the warning.)

Answer

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

This should work:

  1. Move (or unzip) the compiled proto files including model_pb2.py into a directory outside the pydev project.
  2. Add an empty __init__.py file to the same directory as model_pb2.py to ensure it can be imported as a library.
  3. In eclipse, go to Windows -> Preferences -> pydev -> Interpreter
  4. Add the directory with model_pb2.py to the Libraries.
  5. Add model_pb2 to the forced buildins.

If you're not addicted to autocomplete, you may using ctrl+1 to ignoring these errors instead as described in this answer. This was tested with Eclipse Kepler and pydev 2.8.

https://en.xdnf.cn/q/71892.html

Related Q&A

Animating a network graph to show the progress of an algorithm

I would like to animate a network graph to show the progress of an algorithm. I am using NetworkX for graph creation. From this SO answer, I came up with a solution using clear_ouput from IPython.displ…

How to run grpc on ipv4 only

Im going to run a grpc server on IPv4 address like this: server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) protoc_pb2_grpc.add_ProtocServicer_to_server(StockProtocServicer(), server) ser…

Python/PyCharm mark unused import as used

I need to import a resource_rc.py file in my module. It is immediately marked by PyCharm as "unused". Is there a way to mark "unused" imports and also variables, etc. as used in Pyt…

Replacing every 2nd element in the list

I got a 2 dimensional list:[[5, 80, 2, 57, 5, 97], [2, 78, 2, 56, 6, 62], [5, 34, 3, 54, 6, 5, 2, 58, 5, 61, 5, 16]]In which I need to change every second element to 0, starting from first one. So it s…

Are C++-style internal typedefs possible in Cython?

In C++ its possible to declare type aliases that are members of a class or struct:struct Foo {// internal type aliastypedef int DataType;// ... };Is there any way to do the same thing in Cython? Ive t…

How do I use a regular expression to match a name?

I am a newbie in Python. I want to write a regular expression for some name checking. My input string can contain a-z, A-Z, 0-9, and _ , but it should start with either a-z or A-Z (not 0-9 and _ ). I…

python - multiprocessing module

Heres what I am trying to accomplish - I have about a million files which I need to parse & append the parsed content to a single file. Since a single process takes ages, this option is out. Not us…

How to make VSCode always run main.py

I am writing my first library in Python, When developing I want my run code button in VS Code to always start running the code from the main.py file in the root directory. I have added a new configurat…

Why does tesseract fail to read text off this simple image?

I have read mountains of posts on pytesseract, but I cannot get it to read text off a dead simple image; It returns an empty string.Here is the image:I have tried scaling it, grayscaling it, and adjust…

python click subcommand unified error handling

In the case where there are command groups and every sub-command may raise exceptions, how can I handle them all together in one place?Given the example below:import click@click.group() def cli():pass…