What is the nix-way to specify LD_LIBRARY_PATH?

The nix way is not to specify LD_LIBRARY_PATH for pre-built python binaries downloaded from PyPI. You need to either:

  1. Use the pythonXXPackages. and install python packages the nix-way
  2. Do not download binary packages from PyPI and always build from source. pip install --no-binary will build and install the python package from source, using whatever is installed in your nix shell. You need to make sure all build dependencies exists and are proper nix packages. Make sure you DO NOT set LD_LIBRARY_PATH when you do pip install

To illustrate 2, here’s the greenlet python package (which gevent depends on):

[nix-shell] $ pip install greenlet
[nix-shell] $ python -c 'import greenlet'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/me/project/.venv/lib/python3.9/site-packages/greenlet/__init__.py", line 29, in <module>
    from ._greenlet import _C_API # pylint:disable=no-name-in-module
ImportError: libstdc++.so.6: cannot open shared object file: No such file or directory

[nix-shell] $ pip install --force-reinstall --no-binary :all: greenlet
Collecting greenlet
  Downloading greenlet-3.0.3.tar.gz (182 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 182.0/182.0 kB 6.6 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: greenlet
  Building wheel for greenlet (pyproject.toml) ... done
  Created wheel for greenlet: filename=greenlet-3.0.3-cp311-cp311-linux_aarch64.whl size=595251 sha256=0a05212e1a6c202825becf9a94778557c40c3c98f6490c6a404038b484977f27
  Stored in directory: /home/me/.cache/pip/wheels/56/08/ca/e0bd72a4cd850a40d2a51ee67fbd2f3efe5332fe5fe00e2c53
Successfully built greenlet
Installing collected packages: greenlet
Successfully installed greenlet-3.0.3

[nix-shell]$ python -c 'import greenlet; print("success");'
success
5 Likes