Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A module that was compiled using NumPy 1.x cannot be run in NumPy 2.0.0

I installed numpy 2.0.0

enter image description here

pip install numpy==2.0.0

import numpy as np
np.__version__
#2.0.0

then I installed:

pip install opencv-python

Requirement already satisfied: opencv-python in /usr/local/lib/python3.10/dist-packages (4.8.0.76)
Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.10/dist-packages (from opencv-python) (2.0.0)

Then I did:

import cv2

I am getting this error:


A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.10/dist-packages/colab_kernel_launcher.py", line 37, in <module>
    ColabKernelApp.launch_instance()
  File "/usr/local/lib/python3.10/dist-packages/traitlets/config/application.py", line 992, in launch_instance
    app.start()
  File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelapp.py", line 619, in start
    self.io_loop.start()
  File "/usr/local/lib/python3.10/dist-packages/tornado/platform/asyncio.py", line 195, in start
    self.asyncio_loop.run_forever()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
    self._run_once()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
    handle._run()
  File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 685, in <lambda>
    lambda f: self._run_callback(functools.partial(callback, future))
  File "/usr/local/lib/python3.10/dist-packages/tornado/ioloop.py", line 738, in _run_callback
    ret = callback()
  File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 825, in inner
    self.ctx_run(self.run)
  File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 786, in run
    yielded = self.gen.send(value)
  File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 361, in process_one
    yield gen.maybe_future(dispatch(*args))
  File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
    yielded = ctx_run(next, result)
  File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 261, in dispatch_shell
    yield gen.maybe_future(handler(stream, idents, msg))
  File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
    yielded = ctx_run(next, result)
  File "/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py", line 539, in execute_request
    self.do_execute(
  File "/usr/local/lib/python3.10/dist-packages/tornado/gen.py", line 234, in wrapper
    yielded = ctx_run(next, result)
  File "/usr/local/lib/python3.10/dist-packages/ipykernel/ipkernel.py", line 302, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/usr/local/lib/python3.10/dist-packages/ipykernel/zmqshell.py", line 539, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 2975, in run_cell
    result = self._run_cell(
  File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3030, in _run_cell
    return runner(coro)
  File "/usr/local/lib/python3.10/dist-packages/IPython/core/async_helpers.py", line 78, in _pseudo_sync_runner
    coro.send(None)
  File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3257, in run_cell_async
    has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
  File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3473, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-c8ec22b3e787>", line 1, in <cell line: 1>
    import cv2
  File "/usr/local/lib/python3.10/dist-packages/google/colab/_import_hooks/_cv2.py", line 78, in load_module
    cv_module = imp.load_module(name, *module_info)
  File "/usr/lib/python3.10/imp.py", line 245, in load_module
    return load_package(name, filename)
  File "/usr/lib/python3.10/imp.py", line 217, in load_package
    return _load(spec)
  File "/usr/local/lib/python3.10/dist-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/usr/local/lib/python3.10/dist-packages/cv2/__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.10/dist-packages/google/colab/_import_hooks/_cv2.py", line 78, in load_module
    cv_module = imp.load_module(name, *module_info)
  File "/usr/lib/python3.10/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "/usr/lib/python3.10/imp.py", line 343, in load_dynamic
    return _load(spec)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: _ARRAY_API not found
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-c8ec22b3e787> in <cell line: 1>()
----> 1 import cv2

8 frames
/usr/lib/python3.10/imp.py in load_dynamic(name, path, file)
    341         spec = importlib.machinery.ModuleSpec(
    342             name=name, loader=loader, origin=path)
--> 343         return _load(spec)
    344 
    345 else:

ImportError: numpy.core.multiarray failed to import

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.

like image 406
Talha Tayyab Avatar asked Sep 06 '25 17:09

Talha Tayyab


2 Answers

There are two solutions for this error:

1. downgrade your numpy to 1.26.4

pip install numpy==1.26.4

or

pip install "numpy<2.0" 

Make sure to restart your kernel after downgrading numpy



Another option is:

2. install the latest version of the module which is failing*

I had an old version of opencv-python 4.8.0.76

I was able to get this working by installing the latest version of opencv-python by

pip install opencv-python==4.10.0.84

*Some modules may still not work with numpy 2.0

'We expect that some modules will need time to support NumPy 2'

like image 171
Talha Tayyab Avatar answered Sep 08 '25 12:09

Talha Tayyab


NOTE: I know the op question is not what I'm writing but since searching the error led me here, I just wanted to make sure people see this.

For Those who face this problem during import of fresh installed tensorflow:

A module that was compiled using NumPy 1.x cannot be run in NumPy 2.2.4 as it may crash. To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

I can confirm that this is an easy working sample for having latest windows native tensorflow supporting gpu.

Environment is as follows:

  • Windows 11 Pro 24H2 build 26100.3624
  • rtx3060 GPU with installed gpu driver (latest preferably)
  • Anaconda3-2024.10-1

I try to be newbie friendly (as I'm one of those)

  1. After that open Anaconda Navigator head over to Environments section.
  2. From menu on the bottom of the screen click Create, choose a name for your environment, check the Python language and select Python3.10.X-(in my case was 3.10.16 but should be ok if your X is different) and Press green button Create.

NOTE: According to Tensorflow windows-native installation guide and Tested GPU Build Configurations the latest python supported is 3.10 and

Tensorflow GPU will NOT SUPPORT PYTHON > 3.11 and 3.12 and later ON WINDOWS NATIVELY!

(You can Install it using WSL2 following this guide.

  1. After environment creation, you see its activated and there is a little green play button click on it and select Open Terminal to open a cmd (or whatever command line) inside that environment, you can tell by the name of the environment inside a pair of parentheses before the path like below.
(my-tensorflow-env) C:\Users\someone>
  1. Use this command to install cudatoolkit and cudnn easily inside your isolated environment. (for me was two ~650 MB files to download since the versions are fixed, you probably see similar)
conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
  1. Now before installing tensorflow downgrade Numpy to version 1.X.
pip install "numpy<2.0"
  1. Now for the last part install tensorflow.(it may take a while also prepare a reliable connection as pip is vulnerable to connection issue, also don't panic if it connection timed out or whatever, just do the command again nothing is broken)
python -m pip install tensorflow==2.10.0
  1. to test the redemption enter this command:
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

If you see something like:

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

Congrats! enjoy GPU.

like image 31
tfn Avatar answered Sep 08 '25 11:09

tfn