I find this really strange, because, if I hack my now working venv (no nix) install to show me sys.path
like this:
cat .venv/bin/app
#!/home/confus/devel/python/no-frills-nix-python-template/.venv/bin/python
# -*- coding: utf-8 -*-
import re
import sys
print("\n".join(sys.path)) # <--- HACKED
from app.cli import start
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(start())
then I get:
'/home/confus/devel/python/no-frills-nix-python-template/.venv/bin'
'/nix/store/j1c268l85zbpdgbl5wlg0zc73zrfngvq-xonsh-0.14.0/lib/python3.10/site-packages' # <-- contains the `.pth` file which seems to be ignored
'/nix/store/1r6n7v2wam7gkr18gxccpg7p5ywgw551-python3-3.10.12/lib/python310.zip'
'/nix/store/1r6n7v2wam7gkr18gxccpg7p5ywgw551-python3-3.10.12/lib/python3.10'
'/nix/store/1r6n7v2wam7gkr18gxccpg7p5ywgw551-python3-3.10.12/lib/python3.10/lib-dynload'
'/home/confus/devel/python/no-frills-nix-python-template/.venv/lib/python3.10/site-packages'
'/home/confus/devel/python/no-frills-nix-python-template/src' # <-- this makes importing work nonetheless
Goodbye!
but if I do the same in the nix shell, it’s:
[nix-shell:~/devel/python/no-frills-nix-python-template]$ which app
/run/user/1000/tmp.N5Nt9mMfSr/bin/app
[nix-shell:~/devel/python/no-frills-nix-python-template]$ cat /run/user/1000/tmp.N5Nt9mMfSr/bin/app
#!/nix/store/1r6n7v2wam7gkr18gxccpg7p5ywgw551-python3-3.10.12/bin/python3.10
# -*- coding: utf-8 -*-
import re
import sys
print("\n".join(sys.path)) # <---- likewise HACKED
import app
from app.cli import start
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(start())
[nix-shell:~/devel/python/no-frills-nix-python-template]$ app
'/run/user/1000/tmp.N5Nt9mMfSr/bin'
'/run/user/1000/tmp.N5Nt9mMfSr/lib/python3.10/site-packages' # <-- contains the `.pth` file which seems to be ignored
'/nix/store/j1c268l85zbpdgbl5wlg0zc73zrfngvq-xonsh-0.14.0/lib/python3.10/site-packages'
'/nix/store/1r6n7v2wam7gkr18gxccpg7p5ywgw551-python3-3.10.12/lib/python3.10/site-packages'
'/nix/store/66qrgkmha6dmcac29nvxbjsbrh77jlin-python3.10-pip-23.0.1/lib/python3.10/site-packages'
'/nix/store/fj028lay9j9bsj58564bznjviqa4aapk-python3.10-wheel-0.38.4/lib/python3.10/site-packages'
'/nix/store/cr689gv3clfqhbl05pfqd9g9khzdidcb-python3.10-setuptools-67.4.0/lib/python3.10/site-packages'
'/nix/store/1r6n7v2wam7gkr18gxccpg7p5ywgw551-python3-3.10.12/lib/python310.zip'
'/nix/store/1r6n7v2wam7gkr18gxccpg7p5ywgw551-python3-3.10.12/lib/python3.10'
'/nix/store/1r6n7v2wam7gkr18gxccpg7p5ywgw551-python3-3.10.12/lib/python3.10/lib-dynload'
Traceback (most recent call last):
File "/run/user/1000/tmp.N5Nt9mMfSr/bin/app", line 6, in <module>
import app
ModuleNotFoundError: No module named 'app'
The sys.path
in the two cases are different. So naturally, you’d think “there must be the problem, the .pth files must differ”, but when I diff the .pth
files that are under sys.path
for both, they are identical and link to the source directory:
[nix-shell:~/devel/python/no-frills-nix-python-template]$ diff \
--report-identical-files \
/run/user/1000/tmp.N5Nt9mMfSr/lib/python3.10/site-packages/__editable__.no_frills_nix_python_template-0.0.1.pth .venv/lib/python3.10/site-packages/__editable__.no_frills_nix_python_template-0.0.1.pth
Files /run/user/1000/tmp.N5Nt9mMfSr/lib/python3.10/site-packages/__editable__.no_frills_nix_python_template-0.0.1.pth and .venv/lib/python3.10/site-packages/__editable__.no_frills_nix_python_template-0.0.1.pth are identical
So there’s no reason for that import to fail in one case, but not the other, damn it! Setuptools switched to PEP 660 edtiable installs a while ago, so that might be related. There’s legacy options.