Skip to content

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

@codeman38

Description

@codeman38

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

in_venv = env_prefix is not None and conda_env_name != "base"
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

(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

Example pyproject.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

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugSomething isn't working as expectedstatus/triageThis issue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions