[Python] ImportError: Error importing numpy: you should not try to import numpy from its...

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm starting a new Python project, I made the following pyproject.toml file (using poetry):

    [tool.poetry]
    name = "snaqs"
    version = "3.0.0"
    description = "A minimal rewrite of Philips' snaqs for DCDC Tx"
    authors = ["Freek van Hemert <freek@qodon.bio>"]
    readme = "README.md"

    [tool.poetry.dependencies]
    python = "^3.12"
    pandas = "^2.2.3"
    numpy = "^2.2.4"


    [tool.poetry.group.dev.dependencies]
    jupyterlab = "^4.4.0"

    [tool.pytest.ini_options]
    minversion = "6.0"
    addopts = "-ra -q"
    testpaths = [
    "tests",
    ]

    [tool.poetry.group.test.dependencies]
    pytest = "^8.3.5"

    [build-system]
    requires = ["poetry-core"]
    build-backend = "poetry.core.masonry.api"



    I then do poetry install

    Usually I then start testing, but to keep it minimal, I open a python3 shell with python3. Now when I import numpy as np I get this error:

    $ python3
    Python 3.12.8 (main, Dec 3 2024, 18:42:41) [GCC 13.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import numpy as np
    Traceback (most recent call last):
    File "/home/freek/.cache/pypoetry/virtualenvs/snaqs-LIQmBdDd-py3.12/lib/python3.12/site-packages/numpy/_core/__init__.py", line 23, in <module>
    from . import multiarray
    File "/home/freek/.cache/pypoetry/virtualenvs/snaqs-LIQmBdDd-py3.12/lib/python3.12/site-packages/numpy/_core/multiarray.py", line 10, in <module>
    from . import overrides
    File "/home/freek/.cache/pypoetry/virtualenvs/snaqs-LIQmBdDd-py3.12/lib/python3.12/site-packages/numpy/_core/overrides.py", line 7, in <module>
    from numpy._core._multiarray_umath import (
    ImportError: libstdc++.so.6: cannot open shared object file: No such file or directory

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/home/freek/.cache/pypoetry/virtualenvs/snaqs-LIQmBdDd-py3.12/lib/python3.12/site-packages/numpy/__init__.py", line 114, in <module>
    from numpy.__config__ import show_config
    File "/home/freek/.cache/pypoetry/virtualenvs/snaqs-LIQmBdDd-py3.12/lib/python3.12/site-packages/numpy/__config__.py", line 4, in <module>
    from numpy._core._multiarray_umath import (
    File "/home/freek/.cache/pypoetry/virtualenvs/snaqs-LIQmBdDd-py3.12/lib/python3.12/site-packages/numpy/_core/__init__.py", line 49, in <module>
    raise ImportError(msg)
    ImportError:

    IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

    Importing the numpy C-extensions failed. This error can happen for
    many reasons, often due to issues with your setup or how NumPy was
    installed.

    We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

    Please note and check the following:

    * The Python version is: Python3.12 from "/home/freek/.cache/pypoetry/virtualenvs/snaqs-LIQmBdDd-py3.12/bin/python3"
    * The NumPy version is: "2.2.4"

    and make sure that they are the versions you expect.
    Please carefully study the documentation linked above for further help.

    Original error was: libstdc++.so.6: cannot open shared object file: No such file or directory


    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/freek/.cache/pypoetry/virtualenvs/snaqs-LIQmBdDd-py3.12/lib/python3.12/site-packages/numpy/__init__.py", line 119, in <module>
    raise ImportError(msg) from e
    ImportError: Error importing numpy: you should not try to import numpy from
    its source directory; please exit the numpy source tree, and relaunch
    your python interpreter from there.


    It has been popping up for me on various occasions. It maybe good to say that I use NixOS and installed poetry using this shell.nix file:

    { pkgs ? import <nixpkgs> {} }:

    pkgs.mkShell {

    buildInputs = [
    pkgs.python3
    pkgs.poetry
    ];

    }


    There are a lot of reports out there with a lot of advice, ie:


    Some more info:

    $ which python3
    /home/freek/.cache/pypoetry/virtualenvs/snaqs-LIQmBdDd-py3.12/bin/python3
    snaqs-py3.12


    (I believe that snaqs-py3.12 is the name of my poetry virtual env, confusingly it is added to the output of any command... Would love to get rid of it because it breaks ie loops over the output of ls

    I have tried all the tips, but nothing works for me. Any help would be appreciated.

    Thanx.

    Continue reading...

Compartilhe esta Página