Skip to content

Avoid import sentencepiece_model_pb2 in utils.__init__.py - #24689

Merged
ydshieh merged 1 commit into
mainfrom
fix_import_2
Jul 6, 2023
Merged

ydshieh merged 1 commit into
mainfrom
fix_import_2

Conversation

@ydshieh

@ydshieh ydshieh commented Jul 6, 2023

Copy link
Copy Markdown
Collaborator

What does this PR do?

Otherwise, trying to import anything from utils will fail if protobuf is not installed.

More details in the comment.

from . import sentencepiece_model_pb2_new as sentencepiece_model_pb2
else:
# just to get the expected `No module named 'google.protobuf'` error
from . import sentencepiece_model_pb2

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this as I would like to keep the following usage

from (transformers).utils import sentencepiece_model_pb2 as model_pb2

gives the same error

No module named 'google.protobuf'

as before 24599, when protobuf is not in the env.

It turns out that, if we just ignore this else block, the above import statement (when protobuf is missing) will find the corresponding file, and import it directly, and we wil get the expected error as before.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, interesting. Just to make sure I've understood, without this else statement, we still get the correct No module named 'google.protobuf' error still triggers if protobuf isn't in the env?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The __init__ won't have the name sentencepiece_model_pb2, but the file utils/sentencepiece_model_pb2.py exist. So the import will look at that file and try to import.

(I was overthinking, partially I was thinking to have sentencepiece_model_pb2_old.py and sentencepiece_model_pb2_new.py (and need to keep the name sentencepiece_model_pb2 available in the __init__ for backward compatibility)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining 🤗

@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Jul 6, 2023 •

Copy link
Copy Markdown

The documentation is not available anymore as the PR was closed or merged.

@ydshieh
ydshieh requested a review from asartran July 6, 2023 13:38

@asartran asartran left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants