ITADN

Poetry thinks Conda env is active after it's been deactivated

#10770Closedcodeman38 创建于 2026-03-12
kind/bugarea/venv
C
codeman38commented
### Description I am using Anaconda with the `auto_activate_base` option disabled (i.e., `conda config --set auto_activate_base false`), so that Conda is only active when I've explicitly activated it. After activating and then deactivating a Conda environment, this leads to an edge case that causes incorrect behavior in the `poetry env use` command: * Before activating the environment: * System Python is first in the path. * The CONDA_PREFIX variable is unset. * Poetry correctly assumes that a virtualenv is not active, and creates a local venv. * After running `conda activate $HOME/envs/foo`: * $HOME/envs/foo is first in the path. * CONDA_PREFIX is set to $HOME/envs/foo. * Poetry correctly assumes that $HOME/envs/foo is active and uses it. * After running `conda deactivate`: * System Python takes precedence in PATH. * **CONDA_PREFIX is set, but its value is an empty string (!).** * Because CONDA_PREFIX is set (but empty), Poetry thinks a venv is active. * `poetry env use` tries to run `python` with an empty prefix, but cannot find `python` in the path and fails. The problem is due to https://github.com/python-poetry/poetry/blob/0e02be7c1a1aa132035b109e1fe0800676a4d83a/src/poetry/utils/env/env_manager.py#L217 To confirm that a virtualenv is active, it should check that env_prefix is neither None **nor an empty string**. ### Workarounds Manually run `unset CONDA_PREFIX` after `conda deactivate`. ### Poetry Installation Method other ### Operating System Ubuntu 20.04.3 ### Poetry Version Poetry (version 2.3.2) ### Poetry Configuration ```bash session (note: `$USER` in the below is not literal, but redacts my actual username from the server) cache-dir = "/home/$USER/.cache/pypoetry" data-dir = "/home/$USER/.local/share/pypoetry" installer.max-workers = null installer.no-binary = null installer.only-binary = null installer.parallel = true installer.re-resolve = false keyring.enabled = true python.installation-dir = "{data-dir}/python" # /home/$USER/.local/share/pypoetry/python repositories.nlu_artifactory.url = "REDACTED_URL" repositories.nlu_artifactory_upload.url = "REDACTED_URL" requests.max-retries = 0 solver.lazy-wheel = true system-git-client = false virtualenvs.create = true virtualenvs.in-project = true virtualenvs.options.always-copy = false virtualenvs.options.no-pip = false virtualenvs.options.system-site-packages = false virtualenvs.path = "{cache-dir}/virtualenvs" # /home/$USER/.cache/pypoetry/virtualenvs virtualenvs.prompt = "{project_name}-py{python_version}" virtualenvs.use-poetry-python = false ``` ### Python Sysconfig See attached: [sysconfig.txt](https://github.com/user-attachments/files/25949183/sysconfig.txt) ### Example pyproject.toml ```TOML ``` ### Poetry Runtime Logs Log from running `poetry -vvv env use /path/to/python3.13` after activating and deactivating the Conda environment is attached: [poetry_env_use.log](https://github.com/user-attachments/files/25949255/poetry_env_use.log)
关闭于 2026-03-28 4 条评论