Poetry2nix with numpy but encountered errors "libz.so.1: cannot open shared object file: No such file or directory"

I trying to use poetry with python project using nix flake, after try use template of poetry2nix from following commands:

nix flake init --template github:nix-community/poetry2nix

But after add numpy and import it in a python file, I encounter this error:

Traceback (most recent call last):
  File "/home/icslab/.cache/pypoetry/virtualenvs/comparing-pliAX9us-py3.11/lib/python3.11/site-packages/numpy/core/__init__.py", line 24, in <module>
    from . import multiarray
  File "/home/icslab/.cache/pypoetry/virtualenvs/comparing-pliAX9us-py3.11/lib/python3.11/site-packages/numpy/core/multiarray.py", line 10, in <module>
    from . import overrides
  File "/home/icslab/.cache/pypoetry/virtualenvs/comparing-pliAX9us-py3.11/lib/python3.11/site-packages/numpy/core/overrides.py", line 8, in <module>
    from numpy.core._multiarray_umath import (
ImportError: libz.so.1: 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/icslab/.cache/pypoetry/virtualenvs/comparing-pliAX9us-py3.11/lib/python3.11/site-packages/numpy/__init__.py", line 130, in <module>
    from numpy.__config__ import show as show_config
  File "/home/icslab/.cache/pypoetry/virtualenvs/comparing-pliAX9us-py3.11/lib/python3.11/site-packages/numpy/__config__.py", line 4, in <module>
    from numpy.core._multiarray_umath import (
  File "/home/icslab/.cache/pypoetry/virtualenvs/comparing-pliAX9us-py3.11/lib/python3.11/site-packages/numpy/core/__init__.py", line 50, 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.11 from "/home/icslab/.cache/pypoetry/virtualenvs/comparing-pliAX9us-py3.11/bin/python"
  * The NumPy version is: "1.26.1"

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

Original error was: libz.so.1: 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 "/home/icslab/workplaces/journal/comparing/main.py", line 1, in <module>
    import numpy
  File "/home/icslab/.cache/pypoetry/virtualenvs/comparing-pliAX9us-py3.11/lib/python3.11/site-packages/numpy/__init__.py", line 135, 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.

All things I tried:

poetry init
...
poetry add numpy
poetry run python main.py

flake.nix:

{
  description = "Application packaged using poetry2nix";

  inputs = {
    flake-utils.url = "github:numtide/flake-utils";
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    poetry2nix = {
      url = "github:nix-community/poetry2nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, flake-utils, poetry2nix }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        # see https://github.com/nix-community/poetry2nix/tree/master#api for more functions and examples.
        pkgs = nixpkgs.legacyPackages.${system};
        inherit (poetry2nix.lib.mkPoetry2Nix { inherit pkgs; }) mkPoetryApplication;
      in
      {
        packages = {
          myapp = mkPoetryApplication { projectDir = self; };
          default = self.packages.${system}.myapp;
        };

        devShells.default = pkgs.mkShell {
          packages = [ pkgs.poetry ];
        };
      });
}

PS: I try search the solution from this post, they suggest using mkPoetryEnv, but I don’t know how to edit flake.nix to do this thing.

1 Like

I tried to install zlib and solved this problem

how you installed it?

Maybe still relevant. I’m using devenv for my coding environment and installed it there as dependency. But installing it in your nix-shell or directly into your system should work as well.