ValueError: numpy.ndarray size changed, may indicate binary incompatibility

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# ValueError: numpy.ndarray size changed, may indicate binary incompatibility

The "ValueError: numpy.ndarray size changed, may indicate binary incompatibility" occurs because there was a change in NumPy's C API in version 1.20.0.

To solve the error, upgrade your version of the numpy module.

shell
ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject

There was a change in the C API of NumPy in version 1.20.0 which is the cause of the error.

# Upgrade your version of the numpy module

To resolve the issue, upgrade numpy to the latest version.

Open your terminal and run the following command to update your NumPy version.

shell
pip install numpy --upgrade pip3 install numpy --upgrade python -m pip install numpy --upgrade python3 -m pip install numpy --upgrade py -m pip install numpy --upgrade # ๐Ÿ‘‡๏ธ For Anaconda conda update numpy # ๐Ÿ‘‡๏ธ For Jupyter Notebook !pip install numpy --upgrade

upgrade numpy version

The --upgrade option upgrades the specified package to the newest available version.

When you run the command, you might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".

However, even though the error is shown, the package is installed successfully.

# Uninstall and reinstall NumPy

If the error is not resolved, try to uninstall and then reinstall numpy.

shell
# ๐Ÿ‘‡๏ธ Uninstall numpy pip uninstall numpy pip3 uninstall numpy python -m pip uninstall numpy python3 -m pip uninstall numpy py -m pip uninstall numpy # ๐Ÿ‘‡๏ธ For Anaconda conda remove numpy

Now install the latest version of NumPy.

shell
pip install numpy --upgrade pip3 install numpy --upgrade python -m pip install numpy --upgrade python3 -m pip install numpy --upgrade py -m pip install numpy --upgrade # ๐Ÿ‘‡๏ธ For Anaconda conda install -c conda-forge numpy

# Update the contents of your requirements.txt file

If the error is resolved after reinstalling numpy, update your requirements.txt file with the following command.

shell
pip freeze > requirements.txt

The command will update the contents of your requirements.txt file, so that the next time you run pip install -r requirements.txt, your project will be in a working state.

# Reinstalling the pycocotools module

If the error persists, try to reinstall the pycocotools module.

shell
pip uninstall pycocotools pip install --no-cache-dir pycocotools pip3 uninstall pycocotools pip3 install --no-cache-dir pycocotools

Using the --no-cache-dir option disables pip's cache.

If you still get the error, try to pin your pycocotools version to 2.0.0.

shell
pip install pycocotools==2.0.0 pip3 install pycocotools==2.0.0 python -m pip install pycocotools==2.0.0 python3 -m pip install pycocotools==2.0.0

If none of the suggestions helped, try creating a virtual environment.

# Try installing the module with the --no-binary option

Another thing you can try is to install the package with the --no-binary option.

For example, if the error is caused by the hdbscan package, you would run the following command.

shell
pip install hdbscan --no-cache-dir --no-binary :all: pip3 install hdbscan --no-cache-dir --no-binary :all:

When the --no-binary option is set to :all:, it disables all binary packages.

If the error persists, try to upgrade your version of the numba package.

shell
pip install numba --upgrade pip3 install numba --upgrade

# Create a virtual environment

If the error persists, try creating a virtual environment.

  1. Create a virtual environment.
  2. Activate the virtual environment.
  3. Run the pip install command with the virtual environment active.
shell
# ๐Ÿ‘‡๏ธ Use the correct version of Python when creating VENV 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 # ๐Ÿ‘‡๏ธ Install the specific package in the virtual environment pip install opencv-python

If the python -m venv venv command doesn't work, try the following 2 commands:

  • python3 -m venv venv
  • py -m venv venv
Make sure to use the correct command to activate your virtual environment depending on your operating system and your shell.

Your virtual environment will use the version of Python that was used to create it.

If the error is resolved after installing numpy in a virtual environment, update your requirements.txt file with the following command.

shell
pip freeze > requirements.txt

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev