{"record":{"id":"317e877f2994fd1f","repo":"numpy/numpy","slug":"error-importing-numpy-you-should-not-try-to-impor","errorCode":null,"errorMessage":"Error importing numpy: you should not try to import numpy from\n            its source directory; please exit the numpy source tree, and relaunch\n            your python interpreter from there.","messagePattern":"Error importing numpy: you should not try to import numpy from\n            its source directory; please exit the numpy source tree, and relaunch\n            your python interpreter from there\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"critical","filePath":"numpy/__init__.py","lineNumber":116,"sourceCode":"    __NUMPY_SETUP__  # noqa: B018\nexcept NameError:\n    __NUMPY_SETUP__ = False\n\nif __NUMPY_SETUP__:\n    sys.stderr.write('Running from numpy source directory.\\n')\nelse:\n    # Allow distributors to run custom init code before importing numpy._core\n    from . import _distributor_init\n\n    try:\n        from numpy.__config__ import show_config\n    except ImportError as e:\n        if isinstance(e, ModuleNotFoundError) and e.name == \"numpy.__config__\":\n            # The __config__ module itself was not found, so add this info:\n            msg = \"\"\"Error importing numpy: you should not try to import numpy from\n            its source directory; please exit the numpy source tree, and relaunch\n            your python interpreter from there.\"\"\"\n            raise ImportError(msg) from e\n        raise\n\n    from . import _core\n    from ._core import (\n        False_,\n        ScalarType,\n        True_,\n        abs,\n        absolute,\n        acos,\n        acosh,\n        add,\n        all,\n        allclose,\n        amax,\n        amin,\n        any,\n        arange,","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/numpy/numpy/blob/e117b3ca4edacf581f440dccfd7f3242f0312afa/numpy/__init__.py#L98-L134","documentation":"NumPy raises this ImportError when the build-generated module `numpy.__config__` cannot be found, which only happens when you run Python from inside the uninstalled NumPy source checkout. The `__config__.py` file is created at build/install time and is absent in a raw git clone. NumPy uses this as a signal that you are not running an installed copy.","triggerScenarios":"Running `python -c 'import numpy'` or any script from within the numpy source tree directory before running `pip install .` or `meson install`. Also triggered when a packaging tool or editable install left the source dir without generating `__config__.py`.","commonSituations":"Cloning numpy from git and immediately trying to `import numpy` from the repo root; developers debugging numpy itself without setting `__NUMPY_SETUP__`; CI that checks out source but skips the build step.","solutions":["Exit the numpy source directory (cd to your home or project dir) before launching Python, so it picks up the installed numpy.","Install numpy properly first: run `pip install .` (or `meson install`) from the source tree so __config__.py is generated.","If developing numpy, use an editable/dev install: `python -m pip install -e . --no-build-isolation` after installing build deps.","Verify with `python -c 'import numpy; print(numpy.__file__)'` from outside the source dir that the installed copy is used."],"exampleFix":"// before (run from inside numpy source root)\n$ cd numpy-repo/numpy\n$ python -c 'import numpy'   # ImportError\n\n// after\n$ cd ~\n$ python -c 'import numpy; print(numpy.__version__)'","handlingStrategy":"validation","validationCode":"import os, sys\n# detect running from numpy source tree before importing\nhere = os.path.abspath(os.getcwd())\nif os.path.exists(os.path.join(here, 'numpy', '__init__.py')) and \\\n   not os.path.exists(os.path.join(here, 'numpy', '__config__.py')):\n    raise SystemExit('Refusing to import numpy from its unbuilt source tree; cd out and use the installed copy.')","typeGuard":null,"tryCatchPattern":"try:\n    import numpy as np\nexcept ImportError as e:\n    if 'source directory' in str(e):\n        raise SystemExit('Run from outside the numpy source tree; install numpy first.')\n    raise","preventionTips":["Always install numpy (pip install numpy or pip install -e .) before importing it.","Run Python from a project dir outside any cloned numpy checkout.","In CI, separate the build step from the test step and import only after install."],"tags":["import","build","installation","environment"],"analyzedSha":"e117b3ca4edacf581f440dccfd7f3242f0312afa","analyzedAt":"2026-08-07T01:25:31.049Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}