cisco-ie / cisco-gnmi-python

CLI and library wrapping gNMI functionality to ease usage with Cisco implementations in Python programs.
https://pypi.org/project/cisco-gnmi/
Apache License 2.0
40 stars 18 forks source link

Error when importing cisco_gnmi in Python 3.9 or 3.11 #94

Open jashoema opened 9 months ago

jashoema commented 9 months ago

Originally shared via email by JC Rode:

Fresh 3.9 or 3.11 conda environment with a “pip install cisco_gnmi” and then trying to import cisco_gnmi:

(gnmi) ubuntu:/home/cisco/LAB> pip install cisco_gnmi Collecting cisco_gnmi Using cached cisco_gnmi-1.0.16-py3-none-any.whl (52 kB) Collecting grpcio (from cisco_gnmi) Downloading grpcio-1.59.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.0 kB) Collecting protobuf (from cisco_gnmi) Using cached protobuf-4.25.1-cp37-abi3-manylinux2014_x86_64.whl.metadata (541 bytes) Collecting six (from cisco_gnmi) Using cached six-1.16.0-py2.py3-none-any.whl (11 kB) Collecting cryptography (from cisco_gnmi) Using cached cryptography-41.0.7-cp37-abi3-manylinux_2_28_x86_64.whl.metadata (5.2 kB) Collecting cffi>=1.12 (from cryptography->cisco_gnmi) Downloading cffi-1.16.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (1.5 kB) Collecting pycparser (from cffi>=1.12->cryptography->cisco_gnmi) Using cached pycparser-2.21-py2.py3-none-any.whl (118 kB) Using cached cryptography-41.0.7-cp37-abi3-manylinux_2_28_x86_64.whl (4.4 MB) Downloading grpcio-1.59.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.3/5.3 MB 14.3 MB/s eta 0:00:00 Using cached protobuf-4.25.1-cp37-abi3-manylinux2014_x86_64.whl (294 kB) Downloading cffi-1.16.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (443 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 443.4/443.4 kB 25.2 MB/s eta 0:00:00 Installing collected packages: six, pycparser, protobuf, grpcio, cffi, cryptography, cisco_gnmi Successfully installed cffi-1.16.0 cisco_gnmi-1.0.16 cryptography-41.0.7 grpcio-1.59.3 protobuf-4.25.1 pycparser-2.21 six-1.16.0 (gnmi) ubuntu:/home/cisco/LAB> python Python 3.9.18 (main, Sep 11 2023, 13:41:44) [GCC 11.2.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information.

import cisco_gnmi Traceback (most recent call last): File "", line 1, in File "/home/cisco/LAB/miniconda3/envs/gnmi/lib/python3.9/site-packages/cisco_gnmi/init.py", line 27, in from .client import Client File "/home/cisco/LAB/miniconda3/envs/gnmi/lib/python3.9/site-packages/cisco_gnmi/client.py", line 30, in from . import proto File "/home/cisco/LAB/miniconda3/envs/gnmi/lib/python3.9/site-packages/cisco_gnmi/proto/init.py", line 25, in from . import gnmi_pb2_grpc File "/home/cisco/LAB/miniconda3/envs/gnmi/lib/python3.9/site-packages/cisco_gnmi/proto/gnmi_pb2_grpc.py", line 4, in from . import gnmi_pb2 as gnmipb2 File "/home/cisco/LAB/miniconda3/envs/gnmi/lib/python3.9/site-packages/cisco_gnmi/proto/gnmi_pb2.py", line 19, in from . import gnmi_ext_pb2 as gnmiextpb2 File "/home/cisco/LAB/miniconda3/envs/gnmi/lib/python3.9/site-packages/cisco_gnmi/proto/gnmi_ext_pb2.py", line 33, in _descriptor.EnumValueDescriptor( File "/home/cisco/LAB/miniconda3/envs/gnmi/lib/python3.9/site-packages/google/protobuf/descriptor.py", line 789, in new__ _message.Message._CheckCalledFromGeneratedFile() TypeError: Descriptors cannot be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are:

  1. Downgrade the protobuf package to 3.20.x or lower.
  2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates

(gnmi) ubuntu:/home/cisco/LAB> export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (gnmi) ubuntu:/home/cisco/LAB> python Python 3.9.18 (main, Sep 11 2023, 13:41:44) [GCC 11.2.0] :: Anaconda, Inc. on linux Type "help", "copyright", "credits" or "license" for more information. import cisco_gnmi

So there seems to be a workaround if we read the error message but I guess that cisco_gnmi could be updated to prevent this error…

miott commented 9 months ago

Going to commit directly to master unless I can get a code review from someone.