Sometimes, while working with numpy module, we may face Importerror: numba needs numpy 1.21 or less error which occurs when there is a conflict between the versions of NumPy and Numba. The simplest method to get rid of the error is to upgrade the modules. In this short article, we will discuss how we can solve importerror: numba needs numpy 1.21 or less error using various methods. Moreover, we will also try to understand the error so that in the future you will be able to solve such errors by yourself.
Solve Importerror: numba needs numpy 1.21 or less in Python
The reason for getting importerror: numba needs numpy 1.21 or less error is because of the conflict in the versions of numba and NumPy module. Here we will go through some of the solutions which you can follow in order to get rid of the error:
The problem can be solved by:
- Downgrading the NumPy installation
- Upgrading the Numba module
In this article, we will solve the error using the following methods:
- Upgrading the Numba module
- Downgrading the NumPy module
- Reinstalling the modules
- Using virtual environment
Let us now go through each of the methods:
Upgrading the Numba module
One of the simplest ways is to upgrade the numba module. There are various methods to upgrade the numba module.
You can use the pip command to upgrade the numba module:
# if you are using pip version 2
pip install numba --upgrade
# for pip version 3
pip3 install numba --upgrade
Here are some other alternatives to upgrade numba module if the pip is not in the Path:
# for python 2
python -m pip install numba --upgrade
# for python 3
python3 -m pip install numba --upgrade
# for windows
py -m pip install numba --upgrade
If you are jupyter-notebook, you can use the following commands to upgrade numba module.
# if you are using jupyter-notebook
!pip install numba --upgrade
Hopefully, this will now remove the error and you will be able to use the modules without getting errors.
Upgrade the NumPy module
If you are still getting errors even after upgrading the Numba module, then you also need to upgrade the NumPy module as well.
You can use the following pip commands to upgrade the NumPy module.
#If you are using pip
pip install numpy --upgrade
# if you are using pip3
pip3 install numpy --upgrade
If the pip command is not in the PATH then you can use the following alternative commands to upgrade the NumPy module.
# python version 2
python -m pip install numpy --upgrade
# python version 3
python3 -m pip install numpy --upgrade
# alternative for windows
py -m pip install numpy --upgrade
If you are using Jupyter-notebook, then you can use the following commands:
# jupyter notebook
!pip install numpy --upgrade
Now, run the code again, and hopefully, this time you will get rid of the error;
Uninstall the modules
If for some reason the upgrading fails. You can then try to uninstall the modules and then install the correct versions. Let us first uninstall the modules:
# uninstalling numba module
pip uninstall numba
# uninstall numpy module
pip uninstall numpy
Output:
# installing numba
pip install numba==0.53
# installing numpy
pip install numpy==1.21.4
Hopefully, now you will not face Importerror: numba needs numpy 1.21 or less error anymore.
Creating a virtual environment
Another alternative method is to create a new virtual environment, activate it, and then install the all necessary modules there.
Let us first create a virtual environment and then activate it:
# creating venv envrionnment
python -m venv venv
# activate on Unix or MacOS
source venv/bin/activate
# activate on Windows (cmd.exe)
venv\Scripts\activate.bat
# activate on Windows (PowerShell)
venv\Scripts\Activate.ps1
Now install the numba and numpy on the virtual environment:
# installing numba
pip install numba
pip install numpy
Now, you run the code on your virtual environment without getting Importerror: numba needs numpy 1.21 or less error.
Understanding the error – Importerror: numba needs numpy 1.21 or less error
Now let us try to understand the error deeply. There are two parts to the error. The first part shows the error belongs to ImportError which means there is an error and the script couldn’t import the module. The second part gives more details about the modules. In this case, it says that the numba needs NumPy version 1.21 or lesser.
What is ImportError in Python?
This error generally occurs when a class cannot be imported due to one of the following reasons: The imported class is in a circular dependency. The imported class is unavailable or was not created. The imported class name is misspelled. The imported class from a module is misplaced.
What is numba in Python?
Numba is an open-source JIT compiler that translates a subset of Python and NumPy into fast machine code using LLVM, via the llvmlite Python package. It offers a range of options for parallelizing Python code for CPUs and GPUs, often with only minor code changes.
What is the NumPy module in Python?
NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python.
You may also read: No module named cv2 ModuleNotFoundError in Python
Summary
In this short article, we learned how we can solve Importerror: numba needs numpy 1.21 or less which occurs because of the conflict between the versions of the numba and NumPy module. The simplest way to get rid of the error is to upgrade the numba module. In this article we came across 4 different methods to solve Importerror: numba needs numpy 1.21 or less error.