Error importing <module>; When using python and poetry

I want to use poetry and python on a nixos-system like on any other linux, macos or windows system. I dont want to install packages system-wide, since I might need different versions in different projects. And since my team is not using nix at all, I would like for poetry and python to behave like on their system; which implies I dont want to create a nix-shell/nix-env file for each project. However, when installting poetry and python for my user and trying to import certain modules in my code, I get an error like:

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

These errors occure for some pakcages like “numpy” or “pandas”, but they dont appear to occure on all packages, like streamlit.


Here is a short version of how to reproduce the error:
Assume we have a clean nixos-installation like:

sudo nix-shell -p poetry python3 --pure

Running the following command, I can indeed assert that poetry uses default configuration:

poetry config --list
cache-dir = "/root/.cache/pypoetry"
experimental.system-git-client = false
installer.max-workers = null
installer.modern-installation = true
installer.no-binary = null
installer.parallel = true
keyring.enabled = true
solver.lazy-wheel = true
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs"  # /root/.cache/pypoetry/virtualenvs
virtualenvs.prefer-active-python = false
virtualenvs.prompt = "{project_name}-py{python_version}"
warnings.export = true

Lets create a clean poetry project, and add a critical dependancy, like numpy:

poetry new test && cd test && poetry add numpy

Trying to import numpy:

poetry run python
Python 3.13.0rc2 (main, Sep  6 2024, 21:15:21) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy

Will cause the following error

Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/test-gsA8yyPl-py3.13/lib/python3.13/site-packages/numpy/_core/__init__.py", line 23, in <module>
    from . import multiarray
  File "/root/.cache/pypoetry/virtualenvs/test-gsA8yyPl-py3.13/lib/python3.13/site-packages/numpy/_core/multiarray.py", line 10, in <module>
    from . import overrides
  File "/root/.cache/pypoetry/virtualenvs/test-gsA8yyPl-py3.13/lib/python3.13/site-packages/numpy/_core/overrides.py", line 8, in <module>
    from numpy._core._multiarray_umath import (
        add_docstring,  _get_implementing_args, _ArrayFunctionDispatcher)
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 "/root/.cache/pypoetry/virtualenvs/test-gsA8yyPl-py3.13/lib/python3.13/site-packages/numpy/__init__.py", line 114, in <module>
    from numpy.__config__ import show as show_config
  File "/root/.cache/pypoetry/virtualenvs/test-gsA8yyPl-py3.13/lib/python3.13/site-packages/numpy/__config__.py", line 4, in <module>
    from numpy._core._multiarray_umath import (
    ...<3 lines>...
    )
  File "/root/.cache/pypoetry/virtualenvs/test-gsA8yyPl-py3.13/lib/python3.13/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.13 from "/root/.cache/pypoetry/virtualenvs/test-gsA8yyPl-py3.13/bin/python"
  * The NumPy version is: "2.1.1"

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 "<python-input-0>", line 1, in <module>
    import numpy
  File "/root/.cache/pypoetry/virtualenvs/test-gsA8yyPl-py3.13/lib/python3.13/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.

Has anyone an idea of how to fix this.

2 Likes

Not an expert on this, but I managed to make work numpy and other libraries with stdenv and libGL and exporting LD_LIBRARY_PATH.
This is my shell.nix

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = [
    pkgs.python312
    pkgs.poetry
    pkgs.pkg-config
    pkgs.libmysqlclient
    pkgs.python312Packages.setuptools
    pkgs.nodejs
    pkgs.libGL
    pkgs.stdenv.cc.cc.lib
  ];
  LD_LIBRARY_PATH="${pkgs.libGL}/lib/:${pkgs.stdenv.cc.cc.lib}/lib/:${pkgs.glib.out}/lib/";

If you don’t use a shell.nix maybe you can manage to do this inside your configuration file.

1 Like