[英]ModuleNotFoundError: No module named 'gensim'
My goal is to import gensim in Python 3 on Windows.我的目标是在 Windows 上的 Python 3 中导入 gensim。
I am using Python 3.7.2 (checked by running python -V
in Windows command prompt).我正在使用 Python 3.7.2(通过在 Windows 命令提示符下运行python -V
进行检查)。 I installed gensim by running pip install gensim
.我通过运行pip install gensim
。 I checked the installation by running pip freeze
, and saw the line gensim==3.7.3
.我通过运行pip freeze
检查了安装,并看到了gensim==3.7.3
。
Then, I ran the command py
to enter the interactive python mode (still in Windows command prompt).然后,我运行命令py
进入交互式 python 模式(仍在 Windows 命令提示符下)。 I ran the line import gensim
and got the following output:我运行了import gensim
并得到以下输出:
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:我也尝试过from gensim import test
并得到以下输出:
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?如何使用 Python 3 在 Windows 上安装 gensim? How do I test gensim?我如何测试gensim?
I think you have installed it using normal cmd, so it may have installed it on python2.x.我想你已经使用普通的cmd安装了它,所以它可能已经安装在python2.x上。 Install it with anaconda prompt.使用 anaconda 提示符安装它。
Let me know if it worked for you.让我知道它是否对你有用。
I got the same error after installing gensim in Anaconda.在 Anaconda 中安装 gensim 后,我遇到了同样的错误。 It worked only after I re-started the Anaconda: by exiting it, and re-opening it via the command prompt.它仅在我重新启动 Anaconda 后才起作用:退出它,然后通过命令提示符重新打开它。 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.要理解为什么会发生这种情况,您必须知道 Windows 如何找到要运行的可执行文件,以及 Python 软件是如何安装的。
python.exe
is installed in <PYTHON_INSTALL_DIR>
(eg C:\\Python\\3.7
). python.exe
安装在<PYTHON_INSTALL_DIR>
(例如C:\\Python\\3.7
)。pip.exe
and other Python tools (eg pylint
, virtualenv
, pycrust
, etc.) are installed in <PYTHON_INSTALL_DIR>\\Scripts
. pip.exe
和其他 Python 工具(例如pylint
、 virtualenv
、 pycrust
等)安装在<PYTHON_INSTALL_DIR>\\Scripts
。py.exe
is installed in your Windows system directory (eg C:\\Windows
). py.exe
安装在您的 Windows 系统目录中(例如C:\\Windows
)。python
and pip
commands use the modules found in the directory their installed in, they do not look at PATH. python
和pip
命令使用在其安装目录中找到的模块,它们不查看 PATH。So, let's say you have the following Python versions:因此,假设您有以下 Python 版本:
C:\Python\2.7
C:\Python\3.6
C:\Python\3.7
and your PATH environment contains the following directories:并且您的 PATH 环境包含以下目录:
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
.因此,在运行pip
,这些包可能安装在另一个 Python 版本中,然后是运行python
时获得的版本。
To see which versions are (correctly) installed on your system, run py -0p
.要查看系统上(正确)安装了哪些版本,请运行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)一般解决方案(适用于 Windows)
The best thing is not to rely on your system PATH.最好不要依赖您的系统路径。 Use the py
launcher to select the version you want.使用py
启动器选择您想要的版本。 To run the pip
module corresponding to the Python version you want to use, start pip
as a module instead of executable.要运行与您要使用的 Python 版本对应的pip
模块,请将pip
作为模块而不是可执行文件启动。 So instead of:所以而不是:
pip install <package>
run:跑:
py -3.6 -m pip install <package>
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.这是我对您的情况的假设,因为您的操作系统能够识别命令行中的python
和py
命令,这可能意味着您安装了两个单独的 python 版本。
Since, as you mentioned that python -V
shows gensim
as an installed module.因为,正如您提到的那样, python -V
gensim
显示为已安装的模块。 Try opening python interactive interpreter via command python
instead of py
, and import gensim
module in it.尝试通过命令python
而不是py
打开 python 交互式解释器,并在其中导入gensim
模块。
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.很可能您的机器上安装了 > 1 个 python。 To install gensim (or any package) inside python command line, you can run below:要在 python 命令行中安装 gensim(或任何包),您可以在下面运行:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.