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

Poetry fails to use the currently activated environnement #8631

Closed
4 tasks done
laclouis5 opened this issue Nov 7, 2023 · 6 comments · Fixed by #8831
Closed
4 tasks done

Poetry fails to use the currently activated environnement #8631

laclouis5 opened this issue Nov 7, 2023 · 6 comments · Fixed by #8831
Labels
area/venv Related to virtualenv management kind/bug Something isn't working as expected

Comments

@laclouis5
Copy link

  • Poetry version: 1.7.0

  • Python version: 3.11

  • OS version and name: Ubuntu 22.04.3 LTS

  • pyproject.toml: Empty TOML as created by poetry new <project>

  • I am on the latest stable Poetry version, installed using a recommended method.

  • I have searched the issues of this repo and believe that this is not a duplicate.

  • I have consulted the FAQ and blog for any relevant entries or release notes.

  • If an exception occurs when executing a command, I executed it again in debug mode (-vvv option) and have included the output below.

Issue

In some circonstances Poetry fails to use the activated virtual environnement (for instance a Conda environnement). In my experiments, this seems to occur when there is an hyphen (-) in the name of the folder of the project (for instance poetry-project).

To Reproduce

poetry new poetry-project
cd poetry-project

conda create -n poetry-project python=3.11 -y
conda activate poetry-project

poetry env info

This returns:

Virtualenv
Python:         3.10.11
Implementation: CPython
Path:           NA
Executable:     NA

This works correctly if I change the folder name to use an underscore(_) instead of the hyphen (-), for instance poetry_project.

@laclouis5 laclouis5 added kind/bug Something isn't working as expected status/triage This issue needs to be triaged labels Nov 7, 2023
@dimbleby
Copy link
Contributor

dimbleby commented Nov 7, 2023

poetry just looks at environment variables to find the current virtual environment, I don't see anything special about hyphens

# 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")

so you should start there to understand what's actually happening

@dimbleby
Copy link
Contributor

dimbleby commented Nov 7, 2023

yeah, does not reproduce.

I'm starting with this docker file:

# syntax=docker/dockerfile:1.5
FROM continuumio/miniconda3:23.9.0-0

RUN apt-get update && \
    apt-get install \
      --yes \
      --no-install-recommends \
      curl && \
    curl \
      --silent \
      --show-error \
      --location \
      --output install-poetry.py \
      https://install.python-poetry.org && \
    python3 install-poetry.py

ENV PATH="/root/.local/bin:$PATH"

Then the sequence of commands that you give ends with:

(poetry-project) root@16b75389eb12:/poetry-project# poetry env info

Virtualenv
Python:         3.11.5
Implementation: CPython
Path:           /opt/conda/envs/poetry-project
Executable:     /opt/conda/envs/poetry-project/bin/python
Valid:          True

System
Platform:   linux
OS:         posix
Python:     3.11.5
Path:       /opt/conda/envs/poetry-project
Executable: /opt/conda/envs/poetry-project/bin/python3.11

please provide a way to reproduce - or close

@laclouis5
Copy link
Author

Indeed, I'm not able to reproduce the issue reliably neither. I guess its an interference with other tools I use locally (pyenv, conda, etc.).

@laclouis5
Copy link
Author

laclouis5 commented Nov 13, 2023

@dimbleby It happened again on another machine so I took the time to debug this. I think I now have a reproducible example (the issue is not related to hyphens):

poetry new project
cd project
poetry env use 3.11  # Assuming 3.11 is available and supported
poetry env remove --all
conda env create -n project python=3.11 -y
conda activate project
poetry env info

This results in the same issue as mentioned in my first post, i.e. the conda environment is not detected:

Virtualenv
Python:         3.10.11
Implementation: CPython
Path:           NA
Executable:     NA

After debugging a little, it looks like the command poetry env remove --all does not remove the environment from the envs.toml. It only removes the virtual environment directory.

Then, when getting an environment, the EnvManager wrongly assumes that there already is an existing environment:

if envs_file.exists():
envs = envs_file.read()
env = envs.get(base_env_name)
if env:
python_minor = env["minor"]

I believe that here the env local variable should be None. This seems to create an inconsistent state and the system environment is returned instead of the conda one, i.e no virtual environment detected.

The error does not occur removing the virtual environment explicitly (without --all), for instance with poetry env remove 3.11.

@laclouis5 laclouis5 reopened this Nov 13, 2023
@dimbleby
Copy link
Contributor

sounds like you must be about ready to submit a fix...?

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/venv Related to virtualenv management kind/bug Something isn't working as expected
Projects
None yet
3 participants