mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-07-13 21:50:33 +03:00
Python: add sitecustomize.py, listen to NIX_PYTHONPATH
This commit adds a Nix-specific module that recursively adds paths that are on `NIX_PYTHONPATH` to `sys.path`. In order to process possible `.pth` files `site.addsitedir` is used. The paths listed in `PYTHONPATH` are added to `sys.path` afterwards, but they will be added before the entries we add here and thus take precedence. The reason for adding support for this environment variable is that we can set it in a wrapper without breaking support for `PYTHONPATH`.
This commit is contained in:
parent
02afb228e2
commit
46409b5c32
5 changed files with 33 additions and 0 deletions
18
pkgs/development/interpreters/python/sitecustomize.py
Normal file
18
pkgs/development/interpreters/python/sitecustomize.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
"""
|
||||
This is a Nix-specific module for discovering modules built with Nix.
|
||||
|
||||
The module recursively adds paths that are on `NIX_PYTHONPATH` to `sys.path`. In
|
||||
order to process possible `.pth` files `site.addsitedir` is used.
|
||||
|
||||
The paths listed in `PYTHONPATH` are added to `sys.path` afterwards, but they
|
||||
will be added before the entries we add here and thus take precedence.
|
||||
|
||||
Note the `NIX_PYTHONPATH` environment variable is unset in order to prevent leakage.
|
||||
"""
|
||||
import site
|
||||
import os
|
||||
import functools
|
||||
|
||||
paths = os.environ.pop('NIX_PYTHONPATH', None)
|
||||
if paths:
|
||||
functools.reduce(lambda k, p: site.addsitedir(p, k), paths.split(':'), site._init_pathinfo())
|
Loading…
Add table
Add a link
Reference in a new issue