C2W1: Protobuf error

Running the second code block, “import boto3 …”, I got the following error and I could not continue:


TypeError Traceback (most recent call last)
in
1 import boto3
----> 2 import sagemaker
3 import botocore
4
5 config = botocore.config.Config(user_agent_extra=‘dlai-pds/c2/w1’)

/opt/conda/lib/python3.7/site-packages/sagemaker/init.py in
16 import importlib_metadata
17
—> 18 from sagemaker import estimator, parameter, tuner # noqa: F401
19 from sagemaker.amazon.kmeans import KMeans, KMeansModel, KMeansPredictor # noqa: F401
20 from sagemaker.amazon.pca import PCA, PCAModel, PCAPredictor # noqa: F401

/opt/conda/lib/python3.7/site-packages/sagemaker/estimator.py in
26 from six.moves.urllib.parse import urlparse
27 import sagemaker
—> 28 from sagemaker import git_utils, image_uris
29 from sagemaker.analytics import TrainingJobAnalytics
30 from sagemaker.debugger import TensorBoardOutputConfig # noqa: F401 # pylint: disable=unused-import

/opt/conda/lib/python3.7/site-packages/sagemaker/image_uris.py in
20
21 from sagemaker import utils
—> 22 from sagemaker.spark import defaults
23
24 logger = logging.getLogger(name)

/opt/conda/lib/python3.7/site-packages/sagemaker/spark/init.py in
14 from future import absolute_import
15
—> 16 from sagemaker.spark.processing import PySparkProcessor, SparkJarProcessor # noqa: F401

/opt/conda/lib/python3.7/site-packages/sagemaker/spark/processing.py in
33
34 from sagemaker import image_uris
—> 35 from sagemaker.local.image import _ecr_login_if_needed, _pull_image
36 from sagemaker.processing import ProcessingInput, ProcessingOutput, ScriptProcessor
37 from sagemaker.s3 import S3Uploader

/opt/conda/lib/python3.7/site-packages/sagemaker/local/init.py in
14 from future import absolute_import
15
—> 16 from .local_session import ( # noqa: F401
17 file_input,
18 LocalSagemakerClient,

/opt/conda/lib/python3.7/site-packages/sagemaker/local/local_session.py in
21 from botocore.exceptions import ClientError
22
—> 23 from sagemaker.local.image import _SageMakerContainer
24 from sagemaker.local.entities import (
25 _LocalEndpointConfig,

/opt/conda/lib/python3.7/site-packages/sagemaker/local/image.py in
36
37 import sagemaker
—> 38 import sagemaker.local.data
39 import sagemaker.local.utils
40 import sagemaker.utils

/opt/conda/lib/python3.7/site-packages/sagemaker/local/data.py in
24 from six.moves.urllib.parse import urlparse
25
—> 26 import sagemaker.amazon.common
27 import sagemaker.local.utils
28 import sagemaker.utils

/opt/conda/lib/python3.7/site-packages/sagemaker/amazon/common.py in
21 import numpy as np
22
—> 23 from sagemaker.amazon.record_pb2 import Record
24 from sagemaker.deprecations import deprecated_class
25 from sagemaker.deserializers import SimpleBaseDeserializer

/opt/conda/lib/python3.7/site-packages/sagemaker/amazon/record_pb2.py in
50 extension_scope=None,
51 options=_descriptor._ParseOptions(descriptor_pb2.FieldOptions(), _b(“\020\001”)),
—> 52 file=DESCRIPTOR,
53 ),
54 _descriptor.FieldDescriptor(

/opt/conda/lib/python3.7/site-packages/google/protobuf/descriptor.py in new(cls, name, full_name, index, number, type, cpp_type, label, default_value, message_type, enum_type, containing_type, is_extension, extension_scope, options, serialized_options, has_default_value, containing_oneof, json_name, file, create_key)
559 has_default_value=True, containing_oneof=None, json_name=None,
560 file=None, create_key=None): # pylint: disable=redefined-builtin
→ 561 _message.Message._CheckCalledFromGeneratedFile()
562 if is_extension:
563 return _message.default_pool.FindExtensionByName(full_name)

TypeError: Descriptors cannot not 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: Changes announced May 6, 2022 | Protocol Buffers Documentation

What should I do?

1 Like

Hi,

Can you please try and run this argument after the pip installation cell (where you install sagemaker etc.)

!pip install -disable-pip-version-check protobuf==3.20.0
This should fix your problem.

Muhammad

The Import Sagemaker still doesn’t work.

I put this after the pip sagemaker cell. It is successfully installed.
!pip install --disable-pip-version-check protobuf==3.20.0

DEPRECATION: pyodbc 4.0.0-unsupported has a non-standard version number. pip 23.3 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pyodbc or contact the author to suggest that they release a version with a conforming version number. Discussion can be found at Deprecate legacy versions and version specifiers · Issue #12063 · pypa/pip · GitHub
WARNING: Running pip as the ‘root’ user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: 12. Virtual Environments and Packages — Python 3.11.4 documentation

And when I ran import sagemaker. It yielded the same error below.


TypeError Traceback (most recent call last)
in
1 import boto3
----> 2 import sagemaker
3 import pandas as pd
4 import numpy as np
5 import botocore

/opt/conda/lib/python3.7/site-packages/sagemaker/init.py in
16 import importlib_metadata
17
—> 18 from sagemaker import estimator, parameter, tuner # noqa: F401
19 from sagemaker.amazon.kmeans import KMeans, KMeansModel, KMeansPredictor # noqa: F401
20 from sagemaker.amazon.pca import PCA, PCAModel, PCAPredictor # noqa: F401

/opt/conda/lib/python3.7/site-packages/sagemaker/estimator.py in
26 from six.moves.urllib.parse import urlparse
27 import sagemaker
—> 28 from sagemaker import git_utils, image_uris
29 from sagemaker.analytics import TrainingJobAnalytics
30 from sagemaker.debugger import TensorBoardOutputConfig # noqa: F401 # pylint: disable=unused-import

/opt/conda/lib/python3.7/site-packages/sagemaker/image_uris.py in
20
21 from sagemaker import utils
—> 22 from sagemaker.spark import defaults
23
24 logger = logging.getLogger(name)

/opt/conda/lib/python3.7/site-packages/sagemaker/spark/init.py in
14 from future import absolute_import
15
—> 16 from sagemaker.spark.processing import PySparkProcessor, SparkJarProcessor # noqa: F401

/opt/conda/lib/python3.7/site-packages/sagemaker/spark/processing.py in
33
34 from sagemaker import image_uris
—> 35 from sagemaker.local.image import _ecr_login_if_needed, _pull_image
36 from sagemaker.processing import ProcessingInput, ProcessingOutput, ScriptProcessor
37 from sagemaker.s3 import S3Uploader

/opt/conda/lib/python3.7/site-packages/sagemaker/local/init.py in
14 from future import absolute_import
15
—> 16 from .local_session import ( # noqa: F401
17 file_input,
18 LocalSagemakerClient,

/opt/conda/lib/python3.7/site-packages/sagemaker/local/local_session.py in
21 from botocore.exceptions import ClientError
22
—> 23 from sagemaker.local.image import _SageMakerContainer
24 from sagemaker.local.entities import (
25 _LocalEndpointConfig,

/opt/conda/lib/python3.7/site-packages/sagemaker/local/image.py in
36
37 import sagemaker
—> 38 import sagemaker.local.data
39 import sagemaker.local.utils
40 import sagemaker.utils

/opt/conda/lib/python3.7/site-packages/sagemaker/local/data.py in
24 from six.moves.urllib.parse import urlparse
25
—> 26 import sagemaker.amazon.common
27 import sagemaker.local.utils
28 import sagemaker.utils

/opt/conda/lib/python3.7/site-packages/sagemaker/amazon/common.py in
21 import numpy as np
22
—> 23 from sagemaker.amazon.record_pb2 import Record
24 from sagemaker.deprecations import deprecated_class
25 from sagemaker.deserializers import SimpleBaseDeserializer

/opt/conda/lib/python3.7/site-packages/sagemaker/amazon/record_pb2.py in
50 extension_scope=None,
51 options=_descriptor._ParseOptions(descriptor_pb2.FieldOptions(), _b(“\020\001”)),
—> 52 file=DESCRIPTOR,
53 ),
54 _descriptor.FieldDescriptor(

/opt/conda/lib/python3.7/site-packages/google/protobuf/descriptor.py in new(cls, name, full_name, index, number, type, cpp_type, label, default_value, message_type, enum_type, containing_type, is_extension, extension_scope, options, serialized_options, has_default_value, containing_oneof, json_name, file, create_key)
559 else:
560 return _message.default_pool.FindFieldByName(full_name)
→ 561
562 def init(self, name, full_name, index, number, type, cpp_type, label,
563 default_value, message_type, enum_type, containing_type,

TypeError: Descriptors cannot not 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: Changes announced May 6, 2022 | Protocol Buffers Documentation

After adding the import, restart the kernel.

1 Like

@Majid_Namazi
Did it work for you? Could you please mark it as the Solution so others know it worked?

Muhammad.

You could also try this one → !pip install protobuf==3.20.* on the top cell of notebook