Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named 'numpy.char' when importing Word2Vec #3605

Open
TomEinhorn opened this issue Mar 18, 2025 · 6 comments
Open

Comments

@TomEinhorn
Copy link

TomEinhorn commented Mar 18, 2025

Description

When trying to import Word2Vec from gensim in Google Colab, I consistently encounter a "ModuleNotFoundError: No module named 'numpy.char'" error, despite successful installation of gensim and its dependencies.

Environment

  • Platform: Google Colab
  • Python version: 3.11
  • gensim version: 4.3.3
  • numpy version: 1.26.4
  • scipy version: 1.13.1

Steps to reproduce

  1. Install gensim in a Google Colab notebook: !pip install gensim
  2. Try to import Word2Vec: from gensim.models import Word2Vec

Error message

ModuleNotFoundError Traceback (most recent call last)
in <cell line: 0>()
1 #!pip install --no-cache-dir --force-reinstall gensim
2 # !pip install gensim
----> 3 from gensim.models import Word2Vec
4 import os
5 from tqdm import tqdm
9 frames
/usr/local/lib/python3.11/dist-packages/numpy/init.py in getattr(attr)
365 raise AssertionError()
366 except AssertionError:
--> 367 msg = ("The current Numpy installation ({!r}) fails to "
368 "pass simple sanity checks. This can be caused for example "
369 "by incorrect BLAS library being linked in, or by mixing "
ModuleNotFoundError: No module named 'numpy.char'

What I've tried

  1. Installing specific versions of numpy and scipy
  2. Using --no-cache-dir and --force-reinstall flags
  3. Trying multiple versions of gensim
  4. Creating a mock numpy.char module (worked temporarily but then broke again)
@usamashami11
Copy link

Yes, I am also facing issues while importing Word2Vec in Colab, although few days back, it was working fine. Now it is creating issues after installing gensim using !pip install gensim, and is due to Numpy (and/or Scipy) Version mismatch, apparently, or some modification in gensim source code.
I have raised an issue #3606 as well, so please fix it asap!

@gojomo
Copy link
Collaborator

gojomo commented Mar 19, 2025

If in a Colab notebook I install the latest Gensim using…

!pip install --upgrade gensim

…it successfully installs & rolls-back numpy and scipy as needed. But it does not restart the current session (running Python interpreter).

Thus if you immediately try running any code involving packages that have changed out from under the session – like the downgraded numpy etc – you're likely to run into problems. That interpreter may be confused about what packages are currently available.

For example, if I immediately try from gensim.models import Word2Vec, I get an error similar to that you've reported.

However, if I use the Colab Runtime -> Restart Session menu action, the import from Gensim proceeds without a problem.

I could have sworn Colab used to warn & prompt when changing packages required a session restart. I didn't see any such warning just now. But it's still often necessary when changing packages out from under a running Python instance.

@usamashami11
Copy link

usamashami11 commented Mar 20, 2025

@gojomo I tried the upgrade command too, but couldn't find the restart option, earlier, however, after your guidance, I have been able to run the code smoothly. I assume this is the option which you referred to? (Pasting it for visual assistance of others)

Image

Previously, it was automatically fetching the updated packages, so now there is 1 step additional, i.e. manual restarting of runtime.

Alternative Solution

If anyone doesn't want to manually restart the runtime, they can run this code block after !pip install --upgrade gensim to automatically restart the session:

import os
os.kill(os.getpid(), 9)

Concluding this, I have tested both methods, and they work, so, thanks @gojomo once again for the assistance!

@TAUforPython
Copy link

!pip install --upgrade gensim
import os
os.kill(os.getpid(), 9)

It's work of me!
Google Colab environment.

@ARCCer
Copy link

ARCCer commented Mar 20, 2025

!pip install --upgrade gensim import os os.kill(os.getpid(), 9)

It's work of me! Google Colab environment.

great thanks, it works for me

@gojomo
Copy link
Collaborator

gojomo commented Mar 20, 2025

Yes, that's the 'restart session' menu pick I was referring-to. The kill approach might be equivalent, but I'd be more confident in the menu pick keeping the both notebook sides (browser & backend) in sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants