woojung
woojung

Reputation: 531

gensim error: ImportError: No module named 'gensim'

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.

Upvotes: 52

Views: 135972

Answers (19)

Somex Gupta
Somex Gupta

Reputation: 198

I had a similar problem and discovered that a environment variable was creating the problem.
When we start with

jupyter notebook

system-wide environment variables are used where gensim is not installed.

Solution: When we use (base) D:>python -m jupyter notebook the SARC environment is initialised with plan python, which points to the location of the library.

so use

python -m jupyter notebook

if facing an issue with import where import gensim is working fine in the terminal but failing in the notebook

Upvotes: 0

Aman Rangapur
Aman Rangapur

Reputation: 21

Type pip3 install gensim for Mac OS M1 silicon.

Upvotes: 2

Lavanya U
Lavanya U

Reputation: 23

reinstalling the python,I was able to solve this issue

Upvotes: 1

Asad Syed
Asad Syed

Reputation: 3

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)

Upvotes: 0

Sigmund
Sigmund

Reputation: 59

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.

Upvotes: 0

INDRAJITH EKANAYAKE
INDRAJITH EKANAYAKE

Reputation: 4304

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

Upvotes: 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

Upvotes: 0

oklm
oklm

Reputation: 133

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.

Upvotes: 0

luky
luky

Reputation: 2370

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

Upvotes: 0

Grant Shannon
Grant Shannon

Reputation: 5075

Using pip inside Anaconda command prompt worked for me:

(base) C:\Users\ABC>pip install -U gensim

Upvotes: 0

Manh Quang Do
Manh Quang Do

Reputation: 81

import model gensim python3x:

pip install gensim

Upvotes: 3

vinsinraw
vinsinraw

Reputation: 2145

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

Upvotes: 11

Nikhil Nair
Nikhil Nair

Reputation: 361

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.

Upvotes: 4

mAge
mAge

Reputation: 103

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

Upvotes: 3

Ayush Jain
Ayush Jain

Reputation: 566

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.

Upvotes: 2

hexicle
hexicle

Reputation: 2187

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

Upvotes: 18

Aus_10
Aus_10

Reputation: 780

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')    

Upvotes: 1

Max
Max

Reputation: 1363

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.

Upvotes: 0

Tanu
Tanu

Reputation: 1563

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

Upvotes: 45

Related Questions