简体   繁体   中英

ModuleNotFoundError: No module named 'gensim'

My goal is to import gensim in Python 3 on Windows.

I am using Python 3.7.2 (checked by running python -V in Windows command prompt). I installed gensim by running pip install gensim . I checked the installation by running pip freeze , and saw the line gensim==3.7.3 .

Then, I ran the command py to enter the interactive python mode (still in Windows command prompt). I ran the line import gensim and got the following output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'gensim'

I also tried from gensim import test and got the following output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'gensim'

Any suggestions? How do I install gensim on Windows with Python 3? How do I test gensim?

I think you have installed it using normal cmd, so it may have installed it on python2.x. Install it with anaconda prompt.

Let me know if it worked for you.

I got the same error after installing gensim in Anaconda. It worked only after I re-started the Anaconda: by exiting it, and re-opening it via the command prompt. I wanted to share this experience since someone else may meet the same issue.

To understand why this happens, you must know how Windows finds executables to run, and how the Python software is installed.

  • When running a command, Windows searches for an executable in the environment variable PATH. It executes the first one found.
  • python.exe is installed in <PYTHON_INSTALL_DIR> (eg C:\\Python\\3.7 ).
  • pip.exe and other Python tools (eg pylint , virtualenv , pycrust , etc.) are installed in <PYTHON_INSTALL_DIR>\\Scripts .
  • py.exe is installed in your Windows system directory (eg C:\\Windows ).
  • python and pip commands use the modules found in the directory their installed in, they do not look at PATH.

So, let's say you have the following Python versions:

C:\Python\2.7
C:\Python\3.6
C:\Python\3.7

and your PATH environment contains the following directories:

C:\Python\2.7
C:\Python\3.6\Scripts

then, see the following output:

C:\>python -V
Python 2.7.16

C:\>pip -V
pip 19.1.1 from c:\python\3.6\lib\site-packages\pip (python 3.6)

C:\>py -V
Python 3.7.3

So, when running pip , it is possible that the packages are installed in another Python version then the version you'll get when running python .

To see which versions are (correctly) installed on your system, run py -0p . Example output:

C:\>py -0p
Installed Pythons found by py Launcher for Windows
 -3.7-64        C:\Python\3.7-64\python.exe *
 -3.7-32        C:\Python\3.7-32\python.exe
 -3.6-64        C:\Python\3.6-64\python.exe
 -2.7-64        C:\Python\2.7-64\python.exe
 -2.7-32        C:\Python\2.7-32\python.exe

General solution (for Windows)

The best thing is not to rely on your system PATH. Use the py launcher to select the version you want. To run the pip module corresponding to the Python version you want to use, start pip as a module instead of executable. So instead of:

pip install <package>

run:

py -3.6 -m pip install <package>

In Mac, open anaconda navigator, Click on 'Open Terminal option'. If you are using Windows, Run anaconda prompt as administrator and run the following command:

在此处输入图片说明

conda install -c conda-forge gensim

Here's My hypothesis towards your situation, since your OS is able to recognize both python and py commands in commandline, this may mean that you have two separate versions of python installed.

Since, as you mentioned that python -V shows gensim as an installed module. Try opening python interactive interpreter via command python instead of py , and import gensim module in it.

C:\Users> Python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import gensim

see if that works.

PS

I would not recommend having two different compiler versions on a single OS as it creates a lot of commotion, and create incompatibility issues with program's made on one compiler with the other. And makes problems (like you mentioned) a lot more prevalent.

Most probably you have > 1 python installed in your machine. To install gensim (or any package) inside python command line, you can run below:

  • type "python" then enter
  • type "import subprocess" then enter
  • type "subprocess.check_call(["python", '-m', 'pip', 'install', 'gensim'])" then enter

Sample below:

>>> import subprocess
>>> subprocess.check_call(["python", '-m', 'pip', 'install', 'gensim'])
Collecting gensim
..
...
Installing collected packages: smart-open, gensim
Successfully installed gensim-3.7.3 smart-open-1.8.3
0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM