I was using Visual Studio for a long time, but it was becoming too complicated to maintain. Now I tried to move to VS Code, but it throws a number of PyLint error messages that don't make sense to me (and the program still works as expected). These errors happen primarily with Python code generated from a GoogleProtoBuf structure.
For example:
from lbsnstructure.lbsnstructure_pb2 import lbsnPost
def geoaccuracy_within_threshold(post_geoaccuracy, min_geoaccuracy):
"""Checks if geoaccuracy is within or below threshhold defined"""
if min_geoaccuracy == lbsnPost.LATLNG:
allowed_geoaccuracies = [lbsnPost.LATLNG]
elif min_geoaccuracy == lbsnPost.PLACE:
allowed_geoaccuracies = [lbsnPost.LATLNG, lbsnPost.PLACE]
elif min_geoaccuracy == lbsnPost.CITY:
allowed_geoaccuracies = [lbsnPost.LATLNG, lbsnPost.PLACE, lbsnPost.CITY]
else:
return True
# check post geoaccuracy
if post_geoaccuracy in allowed_geoaccuracies:
return True
else:
return False
Throws error message E0602 from pyLint:
Undefined variable 'lbsnPost' pylint (E0602)
lbsnPost: GeneratedProtocolMessageType
However, Google explicitly states that this form of type-referencing is correct:
Enums are expanded by the metaclass into a set of symbolic constants with integer values. So, for example, the constant addressbook_pb2.Person.WORK has the value 2.
I get similar errors all over my code (that works fine). I suspect that this is something that I have written in the wrong convention, but somehow still works. But what is the right convention?
This page seems to discuss the same issue, but none of the solutions work:
Undefined variable from import when using protocol buffers in PyDev
that is, even when doing lbsnpost().LATLNG
(instantiating the protobuf message), I get the same undefined variable error.
The message simply means that VSCode cannot detect the correct path for a Python module. The cause of "Unresolved Import" could be one of the following reason: VSCode is using the wrong Python path. This is often the case if you're running your code against a virtual environment.
To solve unresolved import error in Python, set your Python path in your workspace settings. If you are working with Visual Studio Code and import any library, you will face this error: “unresolved import”. Then reload the VSCode, and it will fix that error.
To do so, open the Command Palette (Ctrl+Shift+P) and enter Preferences: Open User Settings. Then set python.defaultInterpreterPath , which is in the Python extension section of User Settings, with the appropriate interpreter.
I solved my problem. Apparently, pylint has (had?) problems with protobuf compiled python classes. There's a package available that solves this issue.
pip install pylint-protobuf
)"python.linting.pylintArgs": ["--load-plugins", "pylint_protobuf"]
to User Settings in VS CodeNo errors!
For more information, see the VS Code linting Docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With