{"id":209,"library":"numpy","title":"NumPy","description":"Fundamental package for numerical computing in Python. Current version is 2.4.3 (Mar 2026). NumPy 2.0 (Jun 2024) was a landmark major release with ABI breakage, ~100 removed namespace members, and type promotion changes (NEP 50). Packages built against NumPy 1.x will not import with NumPy 2.x.","status":"active","version":"2.4.3","language":"python","source_language":"en","source_url":"https://numpy.org/doc/stable/numpy_2_0_migration_guide.html","tags":["array","numerical","scientific-computing","linear-algebra","math"],"install":[{"cmd":"pip install numpy","lang":"bash","label":"Core"}],"dependencies":[],"imports":[{"note":"np.float_, np.int_, np.complex_, np.bool_, np.object_, np.str_ aliases all removed in NumPy 2.0. Use np.float64, np.int64, np.complex128, np.bool_ → bool, etc.","wrong":"import numpy as np\n\narr = np.array([1.0, 2.0], dtype=np.float_)  # np.float_ removed in 2.0\nscalar = np.float(3.14)             # np.float removed in 1.24","symbol":"numpy","correct":"import numpy as np\n\narr = np.array([1.0, 2.0], dtype=np.float64)\nscalar = np.float64(3.14)"},{"note":"np.random.seed() + np.random.randn() is the legacy RandomState API. The new Generator API (np.random.default_rng()) is preferred and provides better statistical properties.","wrong":"np.random.seed(42)\narr = np.random.randn(100)  # legacy API — deprecated in favor of Generator","symbol":"random","correct":"rng = np.random.default_rng(seed=42)\narr = rng.standard_normal(100)"}],"quickstart":{"code":"import numpy as np\n\n# Array creation\narr = np.array([1, 2, 3, 4], dtype=np.float64)\nmatrix = np.zeros((3, 4), dtype=np.float32)\n\n# Operations\nresult = arr * 2 + 1\ndot = np.dot(matrix.T, matrix)\n\n# Random (new API)\nrng = np.random.default_rng(seed=42)\nsamples = rng.standard_normal((100, 10))\n\n# Type check\nprint(arr.dtype)   # float64\nprint(arr.shape)   # (4,)","lang":"python","description":"Basic array creation and operations. Use new random Generator API."},"warnings":[{"fix":"Upgrade all binary dependencies to versions built against NumPy 2.x. Check https://github.com/numpy/numpy/issues/24300 for ecosystem compatibility status.","message":"ABI break in NumPy 2.0. Packages built against NumPy 1.x will not import with NumPy 2.x — raises ImportError about binary incompatibility. This affected TensorFlow, PyTorch, scipy, and many others at release.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"Replace with explicit dtypes: np.float_ → np.float64, np.int_ → np.intp, np.complex_ → np.complex128, np.bool_ → np.bool_, np.Inf → np.inf, np.NAN → np.nan. Use ruff --select NPY201 to auto-fix.","message":"~100 np namespace members removed in 2.0 including: np.float_, np.int_, np.complex_, np.object_, np.bool_, np.str_, np.long, np.unicode_, np.cfloat, np.Inf, np.Infinity, np.NAN, np.PINF, np.NINF. AttributeError on first use.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"Audit mixed-precision arithmetic. Explicitly cast where float64 precision is required: np.float64(3) + arr or arr.astype(np.float64).","message":"Type promotion rules changed (NEP 50) in NumPy 2.0. Scalar precision is now preserved: np.float32(3) + 3. now returns float32 instead of float64. Code with mixed scalar/array operations may silently produce lower-precision results.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"Access all public members from the main np namespace. Never import from np.core or np._core directly.","message":"np.core namespace renamed to np._core (private) in 2.0. Code importing from np.core (common in older library code) will raise ImportError.","severity":"breaking","affected_versions":">= 2.0"},{"fix":"Use np.random.default_rng(seed) to create a Generator. Replace np.random.randn(n) with rng.standard_normal(n), np.random.randint(a, b) with rng.integers(a, b).","message":"Legacy random API (np.random.seed, np.random.rand, np.random.randn, np.random.randint etc.) is not formally deprecated yet but the new Generator API is strongly preferred. Legacy API has known issues with reproducibility in multiprocessing.","severity":"deprecated","affected_versions":"all"},{"fix":"Use Python float or np.float64 explicitly. Same for np.int → int or np.int64, np.complex → complex or np.complex128.","message":"np.float (Python built-in alias) was removed in NumPy 1.24, not 2.0. Already gone for 2+ years but still extremely common in LLM-generated code.","severity":"gotcha","affected_versions":">= 1.24"},{"fix":"For variable-length strings: arr = np.array(['hello', 'world'], dtype=np.dtypes.StringDType()). np.string_ creates fixed-width byte arrays.","message":"np.string_ is still available but is bytes-based (fixed-width). For variable-length string arrays use np.dtypes.StringDType() (new in 2.0) or Python object arrays.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-12T10:18:08.650Z","next_check":"2026-06-25T00:00:00.000Z","problems":[{"fix":"Upgrade the affected downstream package to a version compiled against NumPy 2.x, or downgrade NumPy to a version compatible with the package (e.e. `pip install 'numpy<2'`). If it's a locally built package, it needs to be recompiled against the current NumPy 2.x installation.","cause":"This error occurs when a Python package (like SciPy, scikit-learn, or others) that depends on NumPy was compiled against a different NumPy ABI version than the one currently installed, often due to the breaking changes introduced in NumPy 2.0.","error":"ValueError: numpy.ufunc size changed, may indicate binary incompatibility. Expected XXX from C header, got YYY from PyObject"},{"fix":"Try uninstalling and reinstalling NumPy (`pip uninstall numpy && pip install numpy`). If the issue persists, explicitly upgrade or downgrade NumPy to a compatible version for your other packages (`pip install 'numpy<2'` or `pip install numpy --upgrade`), or create a clean virtual environment and reinstall all dependencies.","cause":"This often indicates an incompatibility between NumPy and other installed libraries, a corrupted NumPy installation, or multiple conflicting NumPy versions in the environment, which is particularly common with NumPy 2.0's ABI changes.","error":"ImportError: numpy.core.multiarray failed to import"},{"fix":"Install NumPy using pip (`pip install numpy`). If already installed, ensure you are running your script with the Python interpreter where NumPy is installed, or activate the correct virtual environment.","cause":"NumPy is either not installed in the currently active Python environment, or the Python interpreter being used cannot locate the installed NumPy module, often due to multiple Python installations or improper environment setup.","error":"ModuleNotFoundError: No module named 'numpy'"},{"fix":"Replace `np.rank(array)` with `array.ndim` or `np.ndim(array)` to get the number of dimensions. For other removed attributes, consult the NumPy 2.0 migration guide for the correct replacement (e.g., `np.prod` for `np.product`).","cause":"This error occurs because the `numpy.rank` function was deprecated and later removed in NumPy 2.0 (and earlier, deprecated in 1.9.0) to avoid confusion with `numpy.linalg.matrix_rank`. Similarly, other attributes like `numpy.product` were also removed or moved.","error":"AttributeError: module 'numpy' has no attribute 'rank'"},{"fix":"Ensure all array elements have a consistent data type before performing the operation, typically by explicitly casting them using the `.astype()` method (e.g., `arr.astype(float)`). For type promotion issues in NumPy 2.0, explicitly cast to the desired precision or use Python scalars.","cause":"This error indicates an attempt to perform a universal function (ufunc) operation (like addition or subtraction) on arrays with incompatible or mixed data types, or when the data type promotion rules (which changed in NumPy 2.0, NEP 50) result in an unexpected type.","error":"TypeError: ufunc 'add' did not contain a loop with signature matching types"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.2,"mem_mb":6.7,"disk_size":"89.3M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.16,"mem_mb":6.7,"disk_size":"86M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.33,"mem_mb":7.1,"disk_size":"96.8M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.28,"mem_mb":7.1,"disk_size":"92M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.26,"mem_mb":7,"disk_size":"85.2M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.28,"mem_mb":7,"disk_size":"81M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.22,"mem_mb":7.5,"disk_size":"84.6M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.27,"mem_mb":7.5,"disk_size":"80M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.18,"mem_mb":6.4,"disk_size":"97.1M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.17,"mem_mb":6.4,"disk_size":"96M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}