Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

ImportError: you should not try to import numpy from its source directory

Verified FixPython 3.10+NumPy 1.24+Silo: numpy

Quick Fix / Solution Rapide

Change your working directory outside the NumPy Git repository root or install NumPy in editable mode (pip install -e . --no-build-isolation).

Root Cause Analysis

This error occurs when Python tries to import numpy while the current working directory (CWD) is the root folder of the NumPy source code repository, preventing Python from locating the compiled C extension binaries.

1. Python Current Working Directory Precedence in sys.path

By default, Python prepends '' (the current working directory) to sys.path. When you run a script inside a cloned numpy/ repository, Python attempts to load the local uncompiled numpy/__init__.py instead of the compiled package in site-packages.

2. Explicit Source Directory Detection in NumPy

NumPy's __init__.py explicitly detects when it is being imported from an unbuilt source tree without compiled C-extensions (_multiarray_umath) and intentionally raises ImportError: Error importing numpy: you should not try to import numpy from its source directory.

3. Naming Local Folders or Files 'numpy.py'

Creating a personal script named numpy.py or a directory named numpy/ in your project folder will shadow the installed NumPy library.

4. Incomplete In-Place Builds

Developers contributing to NumPy who run git pull without recompiling extension modules via Meson/Ninja will trigger this error.

Reproduction Code (MCVE)

Example: Bug Reproduction
import os

def check_source_directory_import(cwd, source_repo_root):
    if os.path.abspath(cwd) == os.path.abspath(source_repo_root):
        raise ImportError('Error importing numpy: you should not try to import numpy from its source directory')

check_source_directory_import('./numpy_repo', './numpy_repo')

Solution 1: Execute Scripts Outside the NumPy Source Directory

Navigate outside the cloned repository directory before launching Python or executing test scripts.

Example: Recommended Solution
import os
import numpy as np

print(f'Current Working Directory: {os.getcwd()}')
print(f'NumPy successfully loaded from: {np.__file__}')
print(f'NumPy version: {np.__version__}')

Solution 2: Rename Shadowing Local Scripts and Folders

Ensure no file named numpy.py or folder named numpy/ exists in your application project root.

Example: Alternative Solution
import sys

print('Check sys.path order:')
for p in sys.path[:3]:
    print(f' - {p}')
print('Ensure no local script shadows the installed numpy module.')

A very frequent beginner mistake is creating a tutorial script named numpy.py to practice arrays. When running python numpy.py, Python attempts to import the file itself, causing circular import errors or source directory warnings. Always name scripts descriptively, such as array_tutorial.py. Edge cases occur in automated CI/CD pipelines where test runners invoke pytest from the project root instead of installing the package in a clean virtual environment first. Contrast this error with ModuleNotFoundError: No module named 'numpy', which indicates that NumPy is not installed at all.