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

Error when running a uv backend session with active conda environment #801

Closed
jayqi opened this issue Mar 19, 2024 · 2 comments · Fixed by #804
Closed

Error when running a uv backend session with active conda environment #801

jayqi opened this issue Mar 19, 2024 · 2 comments · Fixed by #804
Labels

Comments

@jayqi
Copy link

jayqi commented Mar 19, 2024

Current Behavior

If you have a conda environment active and try to run a session with using uv as a backend and session.install, uv throws an error because it detects both an active conda environment and an active virtual environment:

nox > Command uv pip install ruff failed with exit code 2:
error: Both VIRTUAL_ENV and CONDA_PREFIX are set. Please unset one of them.

Expected Behavior

nox should clear both VIRTUAL_ENV and CONDA_PREFIX environment variables before trying to run a session.

(I am mixing conda environments and virtual environments for a project because my project needs graphviz, which is nice to install with conda. My dev environment is using conda. However, I don't need it for all environments like linting, so it would be nice to be able to use uv for fast sessions.)

Steps To Reproduce

# noxfile.py
import nox


@nox.session(venv_backend="uv")
def lint(session):
    session.install("ruff")
# No conda environment active, no problem
❯ nox -s lint
nox > Running session lint
nox > Creating virtual environment (uv) using python in .nox/lint
nox > uv pip install ruff
nox > Session lint was successful.

# Activate a conda environment, try to run
❯ mamba create -n my-conda-env -y
...redacted for brevity...
❯ mamba activate my-conda-env
❯ nox -s lint
nox > Running session lint
nox > Creating virtual environment (uv) using python in .nox/lint
nox > uv pip install ruff
nox > Command uv pip install ruff failed with exit code 2:
error: Both VIRTUAL_ENV and CONDA_PREFIX are set. Please unset one of them.
nox > Session lint failed.

Environment

- OS: macOS
- Python: 3.12.1
- Nox: 2024.3.2

Anything else?

No response

@jayqi jayqi added the bug label Mar 19, 2024
@jayqi
Copy link
Author

jayqi commented Mar 19, 2024

It looks like manually unsetting the CONDA_PREFIX environment variable is a workaround, e.g.,

@nox.session(venv_backend="uv")
def lint(session):
    session.env.pop("CONDA_PREFIX")
    session.install("ruff")

@jayqi
Copy link
Author

jayqi commented Mar 21, 2024

I just saw astral-sh/uv#2574 was released with uv v0.1.23.

This fixes the specific problem I was having. However, I think the opposite case would still be an issue (active venv, nox session uses conda env and user tries to use uv) and would install things into the wrong environment.

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

Successfully merging a pull request may close this issue.

1 participant