Why some Python package comes with PYTHONPATH and another not?

Hi all,

when building my nix-shell environment, I found that including package pre-commit in mkShell buildInputs results in populated PYTHONPATH shell environment variable, but including Poetry doesn’t. Both are Python packages and are built in very similar way. The only difference is that Poetry declares pythonNamespaces while pre-commit doesn’t, but according my testing (trying to override pre-commit with setting pythonNamespaces) it doesn’t have any impact on resulting PYTHONPATH.

Example:

  1. shell.nix with poetry:
    pkgs.mkShell {
        name = "xyz";
        buildInputs = with pkgs; [
            poetry
        ];
$ echo $PYTHONPATH
[NOTHING] 
  1. shell.nix with pre-commit:
    pkgs.mkShell {
        name = "xyz";
        buildInputs = with pkgs; [
            pre-commit
        ];
$ echo $PYTHONPATH

/nix/store/6kcxf8w39r1gfgbssg8jwigsg4bz21xw-python3.8-pre-commit-2.11.0/lib/python3.8/site-packages:/nix/store/k0x66mivh0dmmiw68xx4qnkcakvdjcw9-python3.8-aspy.yaml-1.3.0/lib/python3.8/site-packages:/nix/store/hqfbrk1q1zjj04990r3frhnxrqb1by8g-python3.8-PyYAML-5.4.1/lib/python3.8/site-packages: ....

Thank you very much.

2 Likes

Looks like it might be PYTHOPATH leaking as described in some other threads but I am still not sure what is causing this especially in this case when one package leaks and other not.

You are looking at pkgs/development/python-modules/poetry/default.nix (used by pkgs.python3.pkgs.poetry) but pkgs.poetry points to pkgs/development/tools/poetry2nix/poetry2nix/pkgs/poetry/default.nix:

If I understand it correctly, poetry is now built by poetry2nix (pkgs/development/tools/poetry2nix/poetry2nix/pkgs/poetry/default.nix) and pkgs/development/python-modules/poetry/default.nix is not used at all for (there is no reference of it in all-packages.nix) ?

1 Like

So I know this is an old thread, but I actually ran into that exact issue again. I have my own Python package (using pkgs.python3Packages.callPackage) which I then convert to an application with pkgs.python3Packages.toPythonApplication. Inserting the latter in a devshell causes pollution of PYTHONPATH. somehow that sounds counter intuitive when using a Python “application”. Was there any progress on this problem?

No progress but the canonical workaround is removing the propagation:

2 Likes

I was confused by this too and the reason PYTHONPATH is propagated in python application is to avoid rebuilds. I added a comment, PR and issue on tracker

1 Like