Python module Gensim error "cannot import name utils" - python

Hi I am using Gensim Word2Vec for word embedding in python.
from gensim.models import Word2Vec, KeyedVectors
But i am getting error like:
from gensim import utils
# cannot import whole gensim.corpora, because that imports wikicorpus...
from gensim.corpora.dictionary import Dictionary
ImportError: cannot import name utils. Thank you

I had a similar issue - In my case it was a dependency issue on scipy. I did the following to reinstall scipy.
pip uninstall scipy
conda install scipy
Hope this helps!

Related

How to import utils from keras_unet

I'm trying to add utils from keras_unet in google colab, but I have a problem.
import tensorflow as tfs
from keras_unet import utils
keras-unet init: TF version is >= 2.0.0 - using tf.keras instead of Keras
ModuleNotFoundError
You must install keras-unet before importing as follows
!pip install keras-unet
from keras_unet import utils
Let us know if the issue still persists. Thanks!

module 'sklearn.preprocessing' has no attribute 'Normalization'

I try to to use the preprocessing method from sklearn. I found other threads with the same issue but it didn't helped me to uninstall my existing sklearn versions. I think my problem is still that I have somewhere on my mac a scikit package. I just wonder how to fix this error?
My imports:
!pip install -q git+https://github.com/tensorflow/docs
import matplotlib.pyplot as plt
import numpy as np
import pandas as p
My code where the error line is dropped:
normalizer = preprocessing.Normalization(axis=-1)
Error:
AttributeError: module 'sklearn.preprocessing' has no attribute 'Normalization'

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

I am trying to import BertTokenizer from the transformers library as follows:
import transformers
from transformers import BertTokenizer
from transformers.modeling_bert import BertModel, BertForMaskedLM
However, I get the following error:
I am using transformers version 3.5.1 because I had a problem with the updated version which can be found here.
Does anyone know how to fix this? Apart from updating the transformers library to its latest version (that will unfortunately cause more errors).
Any help is appreciated!
you can change your code from
transformers.modeling_bert import BertModel, BertForMaskedLM
to
from transformers.models.bert.modeling_bert import BertModel,BertForMaskedLM
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

ModuleNotFoundError: No module named 'allennlp.data.iterators'

In Google colab I am to trying import BucketIterator using:
from allennlp.data.iterators import BucketIterator
But it is raising the same error again and again-
ModuleNotFoundError: No module named 'allennlp.data.iterators
After installing allennlp with the imports:
from allennlp.data.token_indexers import TokenIndexer, SingleIdTokenIndexer
from allennlp.data.tokenizers.character_tokenizer import CharacterTokenizer
from allennlp.data.vocabulary import Vocabulary
from allennlp.modules.seq2vec_encoders import PytorchSeq2VecWrapper
are working fine. Is there a way to solve this issue?
I was facing the same issue.
It won't work, since Iterators are removed from allennlp.
Install a legacy version of allennlp. allennlp is depended upon the PyTorch(torchtext) library. And since torchtext removed iterators form their newer versions allennlp did it too.
You can directly use torchtext.data.BucketIterator(). Use:
pip install torchtext==0.5.0 --user

'from gensim import test' is not importing successfully

I installed gensim, Python library.
I executed the command
Import gensim
It executed without any error.
Then I tried to import test from gensim using the command
from gensim import test
and it showed the following error
Traceback (most recent call last):
File "", line 1, in
from gensim import test
ImportError: cannot import name 'test'
Python site-packages had gensim folder in that.
Any help would be highly appreciated.
As it says: cannot import name 'test'
That means that test is not in gensim package.
You can see package's modules with:
import gensim
print(gensim.__all__) # when package provide a __all__
or
import gensim
import pkgutil
modules = pkgutil.iter_modules(gensim.__path__)
for module in modules:
print(module[1])
Edit:
How can I verify gensim installation ?
try:
import gensim
except NameError:
print('gensim is not installed')
Side note: if you have a file or package named gensim, this will ne imported instead of the real package.
I had similar experience but with scipy. After uninstalling scipy import scipy did not give error. but none of its modules will work. I noticed that scipy folder was still in \site-packages but modules inside it were uninstalled.
When I installed it again it worked fine.
You should also see inside the directory, and try to reinstall or upgrade it.

Categories

Resources