Protobuf: python3 got an unexpected keyword argument 'serialized_options'

Created on 2 Jun 2018  路  15Comments  路  Source: protocolbuffers/protobuf

use the protobuf in python3 on ubuntu14.04 , how to fix this ?
I delete all the serialized_options in the py file and fix this but i can't delete this every time .

    import topic_response_pb2
  File "/home/rocky/GitHub/pyemd/test/topic_response_pb2.py", line 22, in <module>
    serialized_pb=_b('\n\x1aproto/topic_response.proto\x12\x05topic\"#\n\x05Token\x12\x0c\n\x04word\x18\x01 \x01(\t\x12\x0c\n\x04prob\x18\x02 \x01(\x01\"E\n\x05Topic\x12\x10\n\x08topic_id\x18\x01 \x01(\x05\x12\x0c\n\x04prob\x18\x02 \x01(\x01\x12\x1c\n\x06tokens\x18\x03 \x03(\x0b\x32\x0c.topic.Token\"Y\n\rTopicResponse\x12\x0c\n\x04text\x18\x01 \x01(\t\x12\x1c\n\x06tokens\x18\x02 \x03(\x0b\x32\x0c.topic.Token\x12\x1c\n\x06topics\x18\x03 \x03(\x0b\x32\x0c.topic.Topicb\x06proto3')
TypeError: __new__() got an unexpected keyword argument 'serialized_options'

rocky@rocky-ubuntu:~$ protoc --version
libprotoc 3.5.1
rocky@rocky-ubuntu:~$ pip list | grep protobuf
protobuf                           3.5.1 
python question

Most helpful comment

I had the same issue until I realised that checking the pip version with
pip show protobuf
gives you the python 2 version.
Indeed pip3 install -U protobuf did the trick for me.

All 15 comments

I am having the same issue.

```
import engine_pb2
File "/home/dell/viotWare/rpc/grpc/engine_pb2.py", line 22, in
serialized_pb=_b('nx16engine.protox12tinfengine"x1cnx0cInferRequestx12x0cnx04x64x61tax18x01 x01(x0c"x1dnx0bInferResultx12x0enx06resultx18x01 x01(t2Mnx0fEnginex12:nx05Inferx12x17.infengine.InferRequestx1ax16.infengine.InferResult"x00x42x39nx1fviotware.ml.inference.infengineBx0eInfEngineProtoPx01xa2x02x03IENbx06proto3')
TypeError: __new__() got an unexpected keyword argument 'serialized_options'


protoc --version
libprotoc 3.6.0

python --version
Python 3.6.5
```

@davenso I fix this by reinstall python package from source code

This error happens when new generated code is being used with an old runtime, That's a configuration we don't support. Upgrade protobuf runtime to newer version later than your protoc version can solve the error.

For compatibility, we support older generated code with newer protobuf runtime.

@rockyzhengwu I noticed that your protoc version and protobuf runtime version are both 3.5.1. Thus this error should not happen. I checked the 3.5.1 protoc, serialized_options was not released in that version.
Can you double check that your /home/rocky/GitHub/pyemd/test/topic_response_pb2.py file is produced by protoc 3.5.1?

I had the same issue until I realised that checking the pip version with
pip show protobuf
gives you the python 2 version.
Indeed pip3 install -U protobuf did the trick for me.

If protoc is newer than runtime, or if using generated _pb2.py code from other libraries(for example Tensorflow) which is using newer protoc version, it may have this problem. Upgrade the runtime to 3.6.0 or above will solve it

FYI> I got this same error when using Google Cloud Vision api. Occur on this line:

from google.cloud import vision

The problem went away when I took the above advice of doing:

pip3 install -U protobuf

I am facing the same issue while the version of both protoc and protobuf module is same

import tensorflow_hub as hub
Traceback (most recent call last):
File "", line 1, in
File "anaconda3/v5.0.1/lib/python3.6/site-packages/tensorflow_hub/__init__.py", line 32, in
from tensorflow_hub.feature_column import image_embedding_column
File "anaconda3/v5.0.1/lib/python3.6/site-packages/tensorflow_hub/feature_column.py", line 24, in
from tensorflow_hub import image_util
File "anaconda3/v5.0.1/lib/python3.6/site-packages/tensorflow_hub/image_util.py", line 21, in
from tensorflow_hub import image_module_info_pb2
File "anaconda3/v5.0.1/lib/python3.6/site-packages/tensorflow_hub/image_module_info_pb2.py", line 22, in
serialized_pb=_b('n&tensorflow_hub/image_module_info.protox12x0etensorflow_hub"x8ax01nx0fImageModuleInfox12x45nx12x64x65x66x61ult_image_sizex18x01 x01(x0bx32).tensorflow_hub.ImageModuleInfo.InputSizex1ax30ntInputSizex12x0enx06heightx18x01 x01(x05x12rnx05widthx18x02 x01(x05Jx04x08x03x10x04x62x06proto3')
TypeError: __new__() got an unexpected keyword argument 'serialized_options'

$ protoc --version
libprotoc 3.5.1
$ pip list |grep protobuf
protobuf (3.5.1)

Tensorflow has shipped some internal generated code (which is using protoc 3.6.0 above, it is not what protoc version you are using) to opensource. Upgrade the runtime to 3.6.0 or above will solve it

When I run tensorflow with protobuf (3.7.0) on Ubuntu 18.04, I had the same problem. Still didn't find a way to fix the problem yet.

You can either:

  1. upgrade to Protobuf 3.6.0 or
  2. hand delete the "serialized_options=None," argument from the generated *_pb2.py files.

You can either:

  1. upgrade to Protobuf 3.6.0 or
  2. hand delete the "serialized_options=None," argument from the generated *_pb2.py files.

Yes, it works. "serialized_options" matters for :

    has_options:  True if the descriptor has non-default options.  Usually it
        is not necessary to read this -- just call GetOptions() which will
        happily return the default instance.  However, it's sometimes useful
        for efficiency, and also useful inside the protobuf implementation to
        avoid some bootstrapping issues.

Don't know if ok to delete it.

In the requirement of the project, it is said to install protobuf 3.5.1, and I got the error. But, when I changed it to the latest version (protobuf 3.10.0), the problem is solved.

from sklearn.metrics import roc_auc_score,roc_curve,classification_report
from sklearn.model_selection import cross_val_score
from imblearn.over_sampling import SMOTE
from yellowbrick.classifier import roc_auc

A function to use smote

def grid_search_random_forrest_best(dataframe,target):

# splitting the data
x_train,x_val,y_train,y_val = train_test_split(dataframe,target, test_size=0.3, random_state=42)

# Applying Smote on train data for dealing with class imbalance
smote = SMOTE(kind='regular')

X_sm, y_sm =  smote.fit_sample(x_train, y_train)

rfc = RandomForestClassifier(n_estimators=11, max_features='auto', max_depth=8, criterion='entropy',random_state=42)

rfc.fit(X_sm, y_sm)
y_pred = rfc.predict(x_val)
print(classification_report(y_val, y_pred))
print(confusion_matrix(y_val, y_pred))
visualizer = roc_auc(rfc,X_sm,y_sm,x_val,y_val)

grid_search_random_forrest_best(X,y)
||
RESULT:
||
TypeError Traceback (most recent call last)
in
1 from sklearn.metrics import roc_auc_score,roc_curve,classification_report
2 from sklearn.model_selection import cross_val_score
----> 3 from imblearn.over_sampling import SMOTE
4 from yellowbrick.classifier import roc_auc
5

C:ProgramDataAnaconda3libsite-packagesimblearn__init__.py in
35 from . import ensemble
36 from . import exceptions
---> 37 from . import keras
38 from . import metrics
39 from . import over_sampling

C:ProgramDataAnaconda3libsite-packagesimblearnkeras__init__.py in
2 in keras."""
3
----> 4 from ._generator import BalancedBatchGenerator
5 from ._generator import balanced_batch_generator
6

C:ProgramDataAnaconda3libsite-packagesimblearnkeras_generator.py in
38
39
---> 40 ParentClass, HAS_KERAS = import_keras()
41
42 from scipy.sparse import issparse

C:ProgramDataAnaconda3libsite-packagesimblearnkeras_generator.py in import_keras()
28 return tuple(), False
29
---> 30 ParentClassKeras, has_keras_k = import_from_keras()
31 ParentClassTensorflow, has_keras_tf = import_from_tensforflow()
32 has_keras = has_keras_k or has_keras_tf

C:ProgramDataAnaconda3libsite-packagesimblearnkeras_generator.py in import_from_keras()
16 def import_from_keras():
17 try:
---> 18 import keras
19 return (keras.utils.Sequence,), True
20 except ImportError:

C:ProgramDataAnaconda3libsite-packageskeras__init__.py in
1 from __future__ import absolute_import
2
----> 3 from . import utils
4 from . import activations
5 from . import applications

C:ProgramDataAnaconda3libsite-packageskerasutils__init__.py in
4 from . import data_utils
5 from . import io_utils
----> 6 from . import conv_utils
7 from . import losses_utils
8 from . import metrics_utils

C:ProgramDataAnaconda3libsite-packageskerasutilsconv_utils.py in
7 from six.moves import range
8 import numpy as np
----> 9 from .. import backend as K
10
11

C:ProgramDataAnaconda3libsite-packageskerasbackend__init__.py in
----> 1 from .load_backend import epsilon
2 from .load_backend import set_epsilon
3 from .load_backend import floatx
4 from .load_backend import set_floatx
5 from .load_backend import cast_to_floatx

C:ProgramDataAnaconda3libsite-packageskerasbackendload_backend.py in
88 elif _BACKEND == 'tensorflow':
89 sys.stderr.write('Using TensorFlow backend.n')
---> 90 from .tensorflow_backend import *
91 else:
92 # Try and load external backend.

C:ProgramDataAnaconda3libsite-packageskerasbackendtensorflow_backend.py in
3 from __future__ import print_function
4
----> 5 import tensorflow as tf
6 from tensorflow.python.eager import context
7 from tensorflow.python.framework import device as tfdev

C:ProgramDataAnaconda3libsite-packagestensorflow__init__.py in
32
33 # pylint: disable=g-bad-import-order
---> 34 from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
35 from tensorflow.python.tools import module_util as _module_util
36

C:ProgramDataAnaconda3libsite-packagestensorflowpython__init__.py in
50
51 # Protocol buffers
---> 52 from tensorflow.core.framework.graph_pb2 import *
53 from tensorflow.core.framework.node_def_pb2 import *
54 from tensorflow.core.framework.summary_pb2 import *

C:ProgramDataAnaconda3libsite-packagestensorflowcoreframeworkgraph_pb2.py in
14
15
---> 16 from tensorflow.core.framework import node_def_pb2 as tensorflow_dot_core_dot_framework_dot_node__def__pb2
17 from tensorflow.core.framework import function_pb2 as tensorflow_dot_core_dot_framework_dot_function__pb2
18 from tensorflow.core.framework import versions_pb2 as tensorflow_dot_core_dot_framework_dot_versions__pb2

C:ProgramDataAnaconda3libsite-packagestensorflowcoreframeworknode_def_pb2.py in
14
15
---> 16 from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
17
18

C:ProgramDataAnaconda3libsite-packagestensorflowcoreframeworkattr_value_pb2.py in
14
15
---> 16 from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
17 from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
18 from tensorflow.core.framework import types_pb2 as tensorflow_dot_core_dot_framework_dot_types__pb2

C:ProgramDataAnaconda3libsite-packagestensorflowcoreframeworktensor_pb2.py in
14
15
---> 16 from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
17 from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
18 from tensorflow.core.framework import types_pb2 as tensorflow_dot_core_dot_framework_dot_types__pb2

C:ProgramDataAnaconda3libsite-packagestensorflowcoreframeworkresource_handle_pb2.py in
21 syntax='proto3',
22 serialized_options=_b('n030org.tensorflow.frameworkB016ResourceHandleP001Z=github.com/tensorflow/tensorflow/tensorflow/go/core/framework370001001'),
---> 23 serialized_pb=_b('n/tensorflow/core/framework/resource_handle.protox12ntensorflow"rnx13ResourceHandleProtox12x0enx06x64x65vicex18x01 x01(tx12x11ntcontainerx18x02 x01(tx12x0cnx04namex18x03 x01(tx12x11nthash_codex18x04 x01(x04x12x17nx0fmaybe_type_namex18x05 x01(tBnnx18org.tensorflow.frameworkBx0eResourceHandlePx01Z=github.com/tensorflow/tensorflow/tensorflow/go/core/frameworkxf8x01x01x62x06proto3')
24 )
25

TypeError: __init__() got an unexpected keyword argument 'serialized_options'

Upgrading protobuf solved my problem
pip install -U protobuf

Was this page helpful?
0 / 5 - 0 ratings