poetry icon indicating copy to clipboard operation
poetry copied to clipboard

False-positive checks for Miniconda base environment (prevents shadowing by other virtual environments)

Open Dominik1123 opened this issue 5 years ago • 2 comments

  • [x] I am on the latest Poetry version.
  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • [x] If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).
  • OS version and name: Ubuntu 16.04 LTS (4.4.0-184-generic #214-Ubuntu SMP Thu Jun 4 10:14:11 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux)
  • Poetry version: 1.0.9
  • Link of a Gist with the contents of your pyproject.toml file: If needed, you can use this one since it was the one with which I discovered the issue.

Issue

When the Miniconda "base" environment is active then Poetry won't allow shadowing by other environments, e.g. such as created by Nox. In my specific case, I ran a shell with Miniconda "base" active, then I ran tests through Nox where dependencies are installed via Poetry (see this for example; invoke via nox -s tests). Even though the Nox environment was active, Poetry refused to use it because it thought it is inside the Miniconda "base" environment (and hence Poetry created its own environment to install dependencies into). This comes from the following check:

# Check if we are inside a virtualenv or not
# Conda sets CONDA_PREFIX in its envs, see
# https://github.com/conda/conda/issues/2764
env_prefix = os.environ.get("VIRTUAL_ENV", os.environ.get("CONDA_PREFIX"))
conda_env_name = os.environ.get("CONDA_DEFAULT_ENV")
# It's probably not a good idea to pollute Conda's global "base" env, since
# most users have it activated all the time.
in_venv = env_prefix is not None and conda_env_name != "base"

Here in_venv is False no matter if the VIRTUAL_ENV variable is set, in case the Miniconda "base" environment is active too. Instead it should explicitly distinguish between VIRTUAL_ENV set or not when checking the CONDA_DEFAULT_ENV:

try:
    env_prefix = os.environ["VIRTUAL_ENV"]
except KeyError:
    env_prefix = os.environ.get("CONDA_PREFIX")
    conda_env_name = os.environ.get("CONDA_DEFAULT_ENV")
else:
    conda_env_name = None  # This allows VIRTUAL_ENV to shadow the conda base.
in_venv = env_prefix is not None and conda_env_name != "base"

Dominik1123 avatar Jun 16 '20 11:06 Dominik1123

I am having the same problem with a simple venv python environment, which is activated, but poetry refuses to use it. I also confirm the same origin of the problem.

stefanoborini avatar Jun 24 '20 09:06 stefanoborini

I have stumbled across the same issue. A custom environment created using venv is ignored by Poetry due to my environment defaulting to the base Conda environment.

tom-lloyd-bbc avatar Nov 13 '24 16:11 tom-lloyd-bbc