Repository: python-poetry/poetry

Issue:
Poetry 1.7.1 Pipx Installation thinks pipx venv is system env
<!--
    Hi there! Thank you for submitting a bug report!

    Before you submit your issue, please review and follow the instructions at
    https://python-poetry.org/docs/contributing/#reporting-bugs. Fully fill out the template below; insufficient
    information or bad reproduction instructions will impair the ability of others to help you.
-->

<!-- All the below information must be provided for others to understand and help with your issue. -->
- **Poetry version**: 1.7.1
- **Python version**: 3.10.11
- **OS version and name**: Windows 10 Pro
- **pyproject.toml**: https://github.com/tum-esm/pyra/blob/v4.1.2/pyproject.toml

<!-- All the below steps should be completed before submitting your issue. Checked checkbox should look like this: [x] -->
- [x] I am on the [latest](https://github.com/python-poetry/poetry/releases/latest) stable Poetry version, installed using a [recommended method](https://python-poetry.org/docs/#installation).
- [x] I have searched the [issues](https://github.com/python-poetry/poetry/issues) of this repo and believe that this is not a duplicate.
- [x] I have consulted the [FAQ](https://python-poetry.org/docs/faq/) and [blog](https://python-poetry.org/blog/) for any relevant entries or release notes.
- [x] If an exception occurs when executing a command, I executed it again in debug mode (`-vvv` option) and have included the output below.

## Issue
<!-- Now feel free to write your issue, and please be as descriptive as possible! Make sure to include detailed reproduction steps. -->
<!-- Thanks again 🙌 ❤ -->

I installed Poetry 1.7.1 using Pipx. When I run `poetry env info`, instead of showing the system interpreter under "system", it shows the pipx environment. I installed pipx using the system interpreter. I our setup, I have to use the system interpreter, so I rely on `poetry env use system`. But when the system is seen as the .venv poetry uses and my dependencies conflict with poetry, I cannot install them without breaking poetry in the process.

<img width="540" alt="Screenshot 2024-01-09 at 22 44 34" src="https://github.com/python-poetry/poetry/assets/29046316/a4ffe286-742a-4e02-b0ed-ba40f21beccd">

Is this a bug?


Discussion:

Resolving patch:
diff --git a/src/poetry/utils/env/env_manager.py b/src/poetry/utils/env/env_manager.py
--- a/src/poetry/utils/env/env_manager.py
+++ b/src/poetry/utils/env/env_manager.py
@@ -728,17 +728,7 @@ def get_system_env(cls, naive: bool = False) -> Env:
         prefix, base_prefix = Path(sys.prefix), Path(cls.get_base_prefix())
         env: Env = SystemEnv(prefix)
         if not naive:
-            if prefix.joinpath("poetry_env").exists():
-                env = GenericEnv(base_prefix, child_env=env)
-            else:
-                from poetry.locations import data_dir
-
-                try:
-                    prefix.relative_to(data_dir())
-                except ValueError:
-                    pass
-                else:
-                    env = GenericEnv(base_prefix, child_env=env)
+            env = GenericEnv(base_prefix, child_env=env)
 
         return env
 


Test patch:
diff --git a/tests/utils/env/test_env_manager.py b/tests/utils/env/test_env_manager.py
--- a/tests/utils/env/test_env_manager.py
+++ b/tests/utils/env/test_env_manager.py
@@ -14,6 +14,7 @@
 from poetry.core.constraints.version import Version
 
 from poetry.toml.file import TOMLFile
+from poetry.utils.env import GET_BASE_PREFIX
 from poetry.utils.env import GET_PYTHON_VERSION_ONELINER
 from poetry.utils.env import EnvManager
 from poetry.utils.env import IncorrectEnvError
@@ -68,7 +69,7 @@ def check_output(cmd: list[str], *args: Any, **kwargs: Any) -> str:
             return f"/usr/bin/{basename}"
 
         if "print(sys.base_prefix)" in python_cmd:
-            return "/usr"
+            return sys.base_prefix
 
         assert "import sys; print(sys.prefix)" in python_cmd
         return "/prefix"
@@ -140,7 +141,7 @@ def test_activate_in_project_venv_no_explicit_config(
     env = manager.activate("python3.7")
 
     assert env.path == tmp_path / "poetry-fixture-simple" / ".venv"
-    assert env.base == Path("/usr")
+    assert env.base == Path(sys.base_prefix)
 
     m.assert_called_with(
         tmp_path / "poetry-fixture-simple" / ".venv",
@@ -196,7 +197,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
     assert envs[venv_name]["patch"] == "3.7.1"
 
     assert env.path == tmp_path / f"{venv_name}-py3.7"
-    assert env.base == Path("/usr")
+    assert env.base == Path(sys.base_prefix)
 
 
 def test_activate_fails_when_python_cannot_be_found(
@@ -256,7 +257,7 @@ def test_activate_activates_existing_virtualenv_no_envs_file(
     assert envs[venv_name]["patch"] == "3.7.1"
 
     assert env.path == tmp_path / f"{venv_name}-py3.7"
-    assert env.base == Path("/usr")
+    assert env.base == Path(sys.base_prefix)
 
 
 def test_activate_activates_same_virtualenv_with_envs_file(
@@ -296,7 +297,7 @@ def test_activate_activates_same_virtualenv_with_envs_file(
     assert envs[venv_name]["patch"] == "3.7.1"
 
     assert env.path == tmp_path / f"{venv_name}-py3.7"
-    assert env.base == Path("/usr")
+    assert env.base == Path(sys.base_prefix)
 
 
 def test_activate_activates_different_virtualenv_with_envs_file(
@@ -342,7 +343,7 @@ def test_activate_activates_different_virtualenv_with_envs_file(
     assert envs[venv_name]["patch"] == "3.6.6"
 
     assert env.path == tmp_path / f"{venv_name}-py3.6"
-    assert env.base == Path("/usr")
+    assert env.base == Path(sys.base_prefix)
 
 
 def test_activate_activates_recreates_for_different_patch(
@@ -394,7 +395,7 @@ def test_activate_activates_recreates_for_different_patch(
     assert envs[venv_name]["patch"] == "3.7.1"
 
     assert env.path == tmp_path / f"{venv_name}-py3.7"
-    assert env.base == Path("/usr")
+    assert env.base == Path(sys.base_prefix)
     assert (tmp_path / f"{venv_name}-py3.7").exists()
 
 
@@ -442,7 +443,7 @@ def test_activate_does_not_recreate_when_switching_minor(
     assert envs[venv_name]["patch"] == "3.6.6"
 
     assert env.path == tmp_path / f"{venv_name}-py3.6"
-    assert env.base == Path("/usr")
+    assert env.base == Path(sys.base_prefix)
     assert (tmp_path / f"{venv_name}-py3.6").exists()
 
 
@@ -594,7 +595,7 @@ def test_get_prefers_explicitly_activated_virtualenvs_over_env_var(
     env = manager.get()
 
     assert env.path == tmp_path / f"{venv_name}-py3.7"
-    assert env.base == Path("/usr")
+    assert env.base == Path(sys.base_prefix)
 
 
 def test_list(
@@ -932,7 +933,7 @@ def test_create_venv_finds_no_python_executable(
 
     poetry.package.python_versions = "^3.6"
 
-    mocker.patch("sys.version_info", (2, 7, 16))
+    mocker.patch("sys.version_info", (3, 4, 5))
     mocker.patch("shutil.which", return_value=None)
 
     with pytest.raises(NoCompatiblePythonVersionFound) as e:
@@ -966,11 +967,12 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_specific
     mocker.patch(
         "subprocess.check_output",
         side_effect=[
+            sys.base_prefix,
             "/usr/bin/python3",
             "3.5.3",
             "/usr/bin/python3.9",
             "3.9.0",
-            "/usr",
+            sys.base_prefix,
         ],
     )
     m = mocker.patch(
@@ -995,7 +997,7 @@ def test_create_venv_fails_if_no_compatible_python_version_could_be_found(
 
     poetry.package.python_versions = "^4.8"
 
-    mocker.patch("subprocess.check_output", side_effect=["", "", "", ""])
+    mocker.patch("subprocess.check_output", side_effect=[sys.base_prefix])
     m = mocker.patch(
         "poetry.utils.env.EnvManager.build_venv", side_effect=lambda *args, **kwargs: ""
     )
@@ -1021,7 +1023,7 @@ def test_create_venv_does_not_try_to_find_compatible_versions_with_executable(
 
     poetry.package.python_versions = "^4.8"
 
-    mocker.patch("subprocess.check_output", side_effect=["3.8.0"])
+    mocker.patch("subprocess.check_output", side_effect=[sys.base_prefix, "3.8.0"])
     m = mocker.patch(
         "poetry.utils.env.EnvManager.build_venv", side_effect=lambda *args, **kwargs: ""
     )
@@ -1203,10 +1205,12 @@ def mock_check_output(cmd: str, *args: Any, **kwargs: Any) -> str:
             executable = cmd[0]
             if "python3.5" in str(executable):
                 return "3.5.12"
-            else:
-                return "3.7.1"
-        else:
-            return "/usr/bin/python3.5"
+            return "3.7.1"
+
+        if GET_BASE_PREFIX in cmd:
+            return sys.base_prefix
+
+        return "/usr/bin/python3.5"
 
     mocker.patch("shutil.which", side_effect=lambda py: f"/usr/bin/{py}")
     check_output = mocker.patch(