ModuleNotFoundError: No module named 'gensim' - python

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?

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

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> (e.g. C:\Python\3.7).
pip.exe and other Python tools (e.g. pylint, virtualenv, pycrust, etc.) are installed in <PYTHON_INSTALL_DIR>\Scripts.
py.exe is installed in your Windows system directory (e.g. 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>

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.
P.S.
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

Related

import torch ModuleNotFoundError: No module named 'torch'

Help me understand few things, im new with python and all these depended libraries.
I m trying to run a project which is written in python. Git repository can be found here: https://github.com/ifzhang/FairMOT
On anaconda prompt I have been running these commands:
conda create -n FairMOT
conda activate FairMOT
conda install pytorch==1.2.0 torchvision==0.4.0 cudatoolkit=10.0 -c pytorch
cd ${FAIRMOT_ROOT}
pip install -r requirements.txt
All being successfully installed
I have performed all the steps that were listed in the Readme file
Now there is training step which requires to run shell script.
Assuming the fact that shell script would not be called on anaconda prompt, I switched to git bash, and I run the script (keeping in mind of path)
sh experiments/crowdhuman_dla34.sh
it throws error
Sanam#LAPTOP-NPVR76P7 MINGW64 /f/NTNU/Deep learning/Repositories/FairMOT (master)
$ sh experiments/crowdhuman_dla34.sh
Traceback (most recent call last):
File "train.py", line 10, in <module>
import torch
ModuleNotFoundError: No module named 'torch'
Question is: is there any other console where I need to run this command other than git bash? it would definitely not work on anaconda prompt. what im doing wrong?
coming back to anaconda prompt, when I test import there, it worked
(FairMOT) F:\NTNU\Deep learning\Repositories\FairMOT>python
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>
where & how do I run this command? so that it would work?is running on git bash not good?
thanks in advance!!
PS:
I have already tried reinstalling, activation/ deactivation of environment but it does not work
Pls. create a virtual envrionment first, then install all the dependencies there. Use the same venv in anaconda and run the bash script in any terminal with the venv activated.
Also, you can run which python to ensure you are installing and then importing it from same python interpreter

python miniconda install sklearn but not found it

To install sklearn, I installed miniconda in my win10 computer, and in the Anaconda command line, I input:
(C:\Python\Miniconda3) C:\Users\apple>conda install sklearn
and it shows that install successfully
(C:\Python\Miniconda3) C:\Users\apple>conda install scikit-learn
Fetching package metadata ...........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at C:\Python\Miniconda3:
#
scikit-learn 0.18.1 np113py36_1
There are python 2.7 and python 3.6 in my computer, and the default environment is python 3.6. However, I found that there is no sklearn in my python 3.6, when I input this order in python 3.6 command line:
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64
bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sklearn
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import sklearn
ModuleNotFoundError: No module named 'sklearn'
and I input the same order in python 2.7 command line, there is also:
>>> import sklearn
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import sklearn
ImportError: No module named sklearn
however in conda command line, it shows the sklearn has been installed successfully.
Could you please tell me the reason and solve it?
NOTE: It is normally inadvisable to have multiple installations of Python; use virtual environments instead if you require the use of different Python versions.
First, check which python version are you using in the command prompt.
python --verison
It should display Miniconda somewhere in your version information. If not, then it would mean that your python is executing in a different path. You would have to set the PATH variable to point to the location where the Miniconda executables are stored.
Explanation:
Usually, when a fresh installation takes place in Windows, the path for the executables is appended to the PATH variable. Using conda would use the Miniconda (or Anaconda) executables because the normal Python installation would not contain it. If you have multiple installations, your python command would point to the executables of the distribution initially installed and not Miniconda (where you have successfully installed sklearn).
You would have to go to the Environment Variables settings to change the order of the installation.

Unable to install scrapy

I am trying to install scrapy and for that I have installed python 2.7 and conda(as pip was giving me errors that i couldn't solve.) after that I created a separate environment for scrapy named py2 with conda.
With this output I suppose that scrapy is installed successfully on my machine.
[py2] C:\Users\ADMIN>conda install -c https://conda.anaconda.org/scrapinghub scrapy
Fetching package metadata: ......
Solving package specifications: ...........................
# All requested packages already installed.
# packages in environment at C:\dev\Anaconda3\envs\py2:
#
scrapy 1.0.3 py27_2 scrapinghub
Now I make it sure that it is python 2.7 I executed this
[py2] C:\Users\ADMIN>py
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Which is also correct I suppose.
Now still I am unable to do this
>>> import scrapy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named scrapy
which indicates that scrapy is not installed.
Environment : Windows 8 (64 bit)
I am a beginner both in python and scrapy.
There are interestingly a few hacks that are needed to run Scrapy on Windows.
You would need to install unofficial support for Windows in Python, look for PyWin32 section:
http://www.lfd.uci.edu/~gohlke/pythonlibs/ Afterwards remember to run postinstall.py -install as advised on this page.
Depending how you are setup, you might need to install C++ compiler for Python, required to run lxml:
https://www.microsoft.com/en-us/download/details.aspx?id=44266
Please look for similar questions referring to overall Python not only Scrapy e.g. error: Unable to find vcvarsall.bat Depending on how you are setup you will find hints around.
As it was all in virtual environment of conda, while installing scrapy I needed to pass the name of conda virtual environment, except that scrapy would have been installed in the root(I mean outside the environment or for the machine.)
So for py2 virtual environment, after activating this, following command will install scrapy:
conda install -n py2 -c https://conda.anaconda.org/scrapinghub scrapy
Here py2 is environment's name.
This page helped me to figure out what was happening here.
I must say python is not that easy to learn.

I can't call pymongo in the python shell

I'm trying to use pymongo in the IDLE shell on MAC OS X 10.9, but I can only do it in terminal.
when I call import pymongo in IDLE I get the following error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pymongo
ImportError: No module named 'pymongo'
but I use python in terminal I get this:
$ python
Python 2.7.5 (default, Sep 12 2013, 21:33:34)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>>
I can then use it with MongoDB.
Have I made some sort of install error? I'm doing this to learn mongoDB so I'm relatively new to this. Any help is much appreciated.
From the information you supplied in the comments, it appears you have installed a version of Python 3.3.4 on your system but you have installed the PyMongo distribution to an instance of Python 2.7, probably the Apple-supplied system Python 2.7 shipped with OS X 10.9. When you install a third-party package (or "distribution"), it is normally associated only with the Python instance that you used to install it. There are several common ways to install such packages. One way is to use the easy_install command, as is suggested on the PyMongo page. However, the easy_install command is also associated with a particular Python instance. On recent OS X releases, Apple supplies easy_install commands that are associated with and install into the system Pythons. So it's a common pitfall on OS X to install a newer version of Python alongside the system Python but then use the default easy_install command with the result that the package you want ends up installed in the wrong Python version. One solution is to install a separate version of easy_install for each Python version you install. The easy_install command is provided by the setuptools package. However, these days the recommended installer tool for Python is pip which provides more features than easy_install, including the ability to uninstall packages, and is actively supported in the community.
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python3.3 get-pip.py
python3.3 -m pip install pymongo
python3.3 -c "import pymongo; print(pymongo.version)"
-> 2.6.3
You also need to install a version of pip for each Python instance that you use. There are other ways to invoke pip but, by using the way shown above, you know which version of Python you are using and you are less likely to end up with the situation you have now. There are other tools you can use in addition, like virtualenv, but, particularly on OS X, pip should be sufficient to handle most beginning use cases.

Psycopg2 install with pip works but cannot import module on OS X 10.9

I've installed psycopg2 with
pip install psycopg2
and it worked just fine. The install output had a couple of warning along the lines of
In file included from ./psycopg/psycopg.h:33:
./psycopg/config.h:71:13: warning: unused function 'Dprintf' [-Wunused-function]
static void Dprintf(const char *fmt, ...) {}
^
1 warning generated.
but in the end it says
Successfully installed psycopg2
and it also appears when I run pip list.
Now when I try to import it in python I get an error:
$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named psycopg2
Why can't Python import the module if it was successfully installed?
(Python 2.7.5 was installed with Homebrew. psycopg2 was installed with pip.)
OS X comes with Python 2.7.5 already; when you install Python with Homebrew, it puts a newer version in a different place without touching the built-in one. Homebrew's Python also comes with Pip, whereas OS X's doesn't. What's going on here is, you're using Homebrew's pip to install psycopg2, then running OS X's python and trying to import it.
Run /usr/local/bin/python (the full path to Homebrew's Python), and try import psycopg2 from there.
If that works, you need to put /usr/local/bin before /usr/bin in your PATH variable, so that your shell finds Homebrew's Python before the OS X one every time. If you use Bash (the default shell in OS X), you can do this by putting the following in your .bash_profile:
export PATH=/usr/local/bin:$PATH
To make sure that you're running the right Python in scripts, use the following shebang line:
#!/usr/bin/env python
env will search PATH for Python and run the script with the first one it finds, the same as typing python from your shell. Bash scripts and such should inherit the PATH variable and find the right python without changes.
You can also hardcode the path to Homebrew Python in your shebang line (#!/usr/local/bin/python), but this means your script will only work on OS X machines with a Homebrew Python installed, and is best avoided.

Categories