No module named ‘transformers.models’ while trying to import BertTokenizer

We Are Going To Discuss About No module named ‘transformers.models’ while trying to import BertTokenizer. So lets Start this Python Article.

No module named ‘transformers.models’ while trying to import BertTokenizer

  1. How to solve No module named 'transformers.models' while trying to import BertTokenizer

    you can change your code from
    transformers.modeling_bert import BertModel, BertForMaskedLM
    to
    from transformers.models.bert.modeling_bert import BertModel,BertForMaskedLM

  2. No module named 'transformers.models' while trying to import BertTokenizer

    you can change your code from
    transformers.modeling_bert import BertModel, BertForMaskedLM
    to
    from transformers.models.bert.modeling_bert import BertModel,BertForMaskedLM

Solution 1

you can change your code from

transformers.modeling_bert import BertModel, BertForMaskedLM

to

from transformers.models.bert.modeling_bert import BertModel,BertForMaskedLM

Original Author lvjiujin Of This Content

Solution 2

How you call the package depends on how you installed the transformers package.

If you’ve installed it from pip you need to find the directory where the package is installed, for example, if installing with pip with conda as a virtual environment this is where the BertModel package will be stored:

/home/{USER_NAME}/anaconda3/envs/{CONDA_ENV_NAME}/lib/{PYTHON_VERSION}/site-packages/transformers/modeling_bert.py

while the BertTokenizer is automatically called in the init.py, hence can be directly called.

Therefore you should be able to call

from transformers.modeling_bert import BertModel, BertForMaskedLM
from transformers import BertTokenizer

otherwise, if you’ve installed it from source, you need to map to the correct file. For Example, with the following file directory structure:

Code_folder
     transformers #package
         models
             bert
                 modeling_bert.py
     main.py # your file that wants to call the transformer package

Then you can call the following packages in the following way:

from transformers.models.bert.modeling_bert import BertModel,BertForMaskedLM
from transformers import BertTokenizer

Original Author Brian Formento Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment