49

I trying to import gensim with

import gensim

but get the following error

ImportError                               Traceback (most recent call  
last)
<ipython-input-5-50007be813d4> in <module>()
----> 1 import gensim
  2 model = gensim.models.Word2Vec.load_word2vec_format('./model  
/GoogleNews-vectors-negative300.bin', binary=True)

ImportError: No module named 'gensim'

I installed gensim in python. I use genssim for word2vec.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
woojung
  • 501
  • 1
  • 4
  • 4

18 Answers18

42

Install gensim using:

pip install -U gensim

Or, if you have instead downloaded and unzipped the source tar.gz package, then run:

python setup.py test
python setup.py install
Tanu
  • 1,503
  • 12
  • 21
18

If using Python3 be sure to use pip3 instead of pip for installing gensim.

hexicle
  • 2,121
  • 2
  • 24
  • 31
10

On Jupyter notebook, following worked for me

!python -m pip install -U gensim

Alternatively, run Anaconda prompt as administrator and execute the following

(base) C:\Windows\system32>conda install -c conda-forge gensim
vinsinraw
  • 2,003
  • 1
  • 16
  • 18
4

I did a pip install gensim --user and it worked. The problem I was having with conda install gensim and pip -U install gensim was that it was not able to modify the environment variable at the end of the install.

Nikhil Nair
  • 331
  • 2
  • 2
3

My solution is for Windows 10, Anaconda. Where I want to use gensim with Spyder.

Solution: Use Anaconda Navigator, and install package from there: Open Anaconda Navigator -> Environments (base) -> not installed (packages) -> (search for) gensim -> check the gensim option from the drop down list-> Press apply button -> (wait for a while, it will search other dependencies, then press the button one more time to install required package)

Scree shot of Anaconda Navigator

  • Repeat above for word2vec

History: On anaconda command prompt, using conda command, I installed gensim. Every thing looks perfect but it was even not imported, "import gensim", in command prompt.

  • Bonus: Same is true for tensorflow
mAge
  • 103
  • 1
  • 9
3

import model gensim python3x:

pip install gensim
2

As mentioned by @Burhan Khalid in the comments, don't name your file gensim.py as it will look in local folder first for gensim and consider it as what you are trying to import.

PS: I wrote this here as people tend to skip the comments. If it helped, please upvote the original comment.

Ayush Jain
  • 518
  • 5
  • 8
1

To Tanu's point, first guess would be you're not in the correct directory. Below is the first thing I would check.

import sys, os
# */site-packages is where your current session is running its python out of
site_path = ''
for path in sys.path:
    if 'site-packages' in path.split('/')[-1]:
        print(path)
        site_path = path
# search to see if gensim in installed packages
if len(site_path) > 0:
    if not 'gensim' in os.listdir(site_path):
        print('package not found')
    else:
        print('gensim installed')    
Aus_10
  • 670
  • 7
  • 15
1

reinstalling the python,I was able to solve this issue

Lavanya U
  • 23
  • 3
0

Does 'gensim' appear in the packages shown by the command pip freeze? If not, you may not have activated the 'environment' with the necessary packages, in your working shell/IDE.

Max
  • 1,283
  • 9
  • 20
0

Using pip inside Anaconda command prompt worked for me:

(base) C:\Users\ABC>pip install -U gensim
Grant Shannon
  • 4,709
  • 1
  • 46
  • 36
0

i had this error because i ran "python" not "python3" i always do this from time to time.

luky
  • 2,263
  • 3
  • 22
  • 40
0

If you are using a virtual environment, check if gensim is installed with the following command:pip list. If it is not installed then install it: pip install -U gensim or pip install gensim.

Also if you are using Jupyter notebook, verify if gensim is install in the python kernel you are using.

oklm
  • 135
  • 1
  • 10
0

The mirror link is slow guyz. Try it manually Download the gensim file from https://pypi.org/project/gensim/#files and extract it by Winrar then go inside the folder type python setup.py install it was success for me

0

If you are trying to install genism for the Jupyter notebook and all the above answers are not working, try installing genism using conda-forge channel

conda install -c conda-forge genism

Here I use the -c flag to give the channel name. If channels are new to you I would like to refer you to this question on Stackoverflow

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63
0

If you have an anaconda environment running, try deactivating that and installing gensim again.

After doing it that way I was able to import it in jupyter notebook.

Sigmund
  • 69
  • 1
  • 9
0

first, you have to run the command !pip install gensim==3.6.0

then run these commands

from gensim.models import KeyedVectors

from gensim.models import word2vec

model = KeyedVectors.load_word2vec_format('./model/GoogleNews-  
vectors-negative300.bin', binary=True)
Jonathan Ciapetti
  • 1,261
  • 3
  • 11
  • 16
Asad Syed
  • 1
  • 2
0

Type pip3 install gensim for Mac OS M1 silicon.