Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nix venv fails to detect python version #1258

Open
asgeirn opened this issue Mar 22, 2024 · 3 comments
Open

Nix venv fails to detect python version #1258

asgeirn opened this issue Mar 22, 2024 · 3 comments
Labels

Comments

@asgeirn
Copy link

asgeirn commented Mar 22, 2024

Describe the bug
I am creating a venv using nix-shell. Using layout python in .direnv yields the following errors:

direnv: loading ~/src/asgeirn/prices/.envrc
environment:850: python: command not found
direnv: Could not find python's version

If I manually activate the venv before cd'ing into the directory, it works as expected.

To Reproduce

  1. nix-shell -p python39
  2. python -m venv .venv
  3. source .venv/bin/activate
  4. pip install -r requirements.txt
  5. echo layout python > .envrc
  6. In a new shell, direnv allow

Expected behavior
The venv should activate, and which python3.9 should point to .venv/bin/python3.9

Environment

  • OS: MacOS Sonoma 14.3.1 (23D60)
  • Shell: zsh 5.9
  • Direnv version 2.34.0
  • nix-shell (Nix) 2.20.4

Additional context

ls -l .venv/bin
total 88
-rw-r--r--  1 asgeir  staff  8834 22 mar 11:27 Activate.ps1
-rw-r--r--  1 asgeir  staff  1914 22 mar 11:27 activate
-rw-r--r--  1 asgeir  staff   863 22 mar 11:27 activate.csh
-rw-r--r--  1 asgeir  staff  2003 22 mar 11:27 activate.fish
-rwxr-xr-x  1 asgeir  staff   249 22 mar 11:28 f2py
-rwxr-xr-x  1 asgeir  staff   266 22 mar 11:28 normalizer
-rwxr-xr-x  1 asgeir  staff   254 22 mar 11:27 pip
-rwxr-xr-x  1 asgeir  staff   254 22 mar 11:27 pip3
-rwxr-xr-x  1 asgeir  staff   254 22 mar 11:27 pip3.9
lrwxr-xr-x  1 asgeir  staff    69 22 mar 11:27 python -> /nix/store/p74zff3wk6mcrs0y8vr848yhj6nxic8h-python3-3.9.18/bin/python
lrwxr-xr-x  1 asgeir  staff     6 22 mar 11:27 python3 -> python
lrwxr-xr-x  1 asgeir  staff     6 22 mar 11:27 python3.9 -> python
@asgeirn asgeirn added the Bug label Mar 22, 2024
@asgeirn
Copy link
Author

asgeirn commented Mar 23, 2024

This .envrc works, but is this the intended way of doing it?

source .venv/bin/activate
layout python

@steinybot
Copy link

Looks like it uses python to figure out the python version and virtual environment to use:

read -r python_version ve <<<$($python -c "import importlib.util as u, platform as p;ve='venv' if u.find_spec('venv') else ('virtualenv' if u.find_spec('virtualenv') else '');print('.'.join(p.python_version_tuple()[:2])+' '+ve)")

@rybaz
Copy link

rybaz commented Jun 10, 2024

Ended up here because I had the same issue - is there a way to determine the python version and include it as part of the direnv instead of having to install it on the system prior? I don't really want to have python installed system-wide if I don't have to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants