{"id":"cc7530e4865aefec","repo":"python-poetry/poetry","slug":"env-env-name-doesn-t-belong-to-this-project","errorCode":null,"errorMessage":"Env {env_name} doesn't belong to this project.","messagePattern":"Env (.+?) doesn't belong to this project\\.","errorType":"exception","errorClass":"IncorrectEnvError","httpStatus":null,"severity":"error","filePath":"src/poetry/utils/env/env_manager.py","lineNumber":291,"sourceCode":"        Check if env name starts with projects name.\n\n        This is done to prevent action on other project's envs.\n        \"\"\"\n        return env.startswith(base_env_name)\n\n    def remove(self, python: str) -> Env:\n        python_path = Path(python)\n        if python_path.is_file():\n            # Validate env name if provided env is a full path to python\n            try:\n                env_dir = subprocess.check_output(\n                    [python, \"-c\", GET_ENV_PATH_ONELINER], text=True, encoding=\"locale\"\n                ).strip(\"\\n\")\n                env_name = Path(env_dir).name\n                if not self.check_env_is_for_current_project(\n                    env_name, self.base_env_name\n                ):\n                    raise IncorrectEnvError(env_name)\n            except CalledProcessError as e:\n                raise EnvCommandError(e)\n\n        if self.check_env_is_for_current_project(python, self.base_env_name):\n            venvs = self.list()\n            for venv in venvs:\n                if venv.path.name == python:\n                    # Exact virtualenv name\n                    if self.envs_file.exists():\n                        venv_minor = \".\".join(str(v) for v in venv.version_info[:2])\n                        self.envs_file.remove_section(self.base_env_name, venv_minor)\n\n                    self.remove_venv(venv.path)\n\n                    return venv\n\n            raise ValueError(\n                f'<warning>Environment \"{python}\" does not exist.</warning>'","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/python-poetry/poetry/blob/92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54/src/poetry/utils/env/env_manager.py#L273-L309","documentation":"Raised as IncorrectEnvError in EnvManager.remove when the user passes a full path to a python executable and the env directory that interpreter reports does not begin with the current project's base_env_name. Poetry derives project ownership from the env name prefix and refuses to operate on another project's virtualenv.","triggerScenarios":"Calling 'poetry env remove /path/to/some/python' where that python lives in a venv belonging to a different project (env_dir.name does not start with this project's base_env_name, checked at env_manager.py:288-291).","commonSituations":"Pointing env remove at a globally-installed python, a venv from another project, or a shared interpreter; running the command from the wrong project directory; project path changed so base_env_name (which is hashed from cwd) no longer matches the previously-created env.","solutions":["Confirm you are running the command from the correct project directory (where pyproject.toml lives).","If the project moved, the env name prefix changed - recreate with 'poetry env remove' by its listed name or 'poetry env list' then remove by version.","Remove the foreign env manually with its owning project rather than from here.","Run 'poetry env list' to see which envs belong to this project and remove one of those."],"exampleFix":"// before - path points into another project's venv\nmanager.remove(\"/home/u/other-project/.venv/bin/python\")  # IncorrectEnvError\n\n// after - remove by the env name listed for THIS project\nmanager.remove(\"myproject-py3.11\")","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef env_belongs_to_project(manager, python_path: str) -> bool:\n    p = Path(python_path)\n    if not p.is_file():\n        return False\n    import subprocess\n    try:\n        env_dir = subprocess.check_output(\n            [python_path, \"-c\", \"import sys; print(sys.prefix)\"], text=True\n        ).strip()\n    except subprocess.CalledProcessError:\n        return False\n    return Path(env_dir).name.startswith(manager.base_env_name)","typeGuard":"from poetry.utils.env.exceptions import IncorrectEnvError, EnvError\n\ndef is_incorrect_env(e: Exception) -> bool:\n    return isinstance(e, IncorrectEnvError) or (isinstance(e, EnvError) and \"belong to this project\" in str(e))","tryCatchPattern":"from poetry.utils.env.exceptions import IncorrectEnvError\n\ntry:\n    manager.remove(python_path)\nexcept IncorrectEnvError as e:\n    raise SystemExit(f\"Refusing to remove env not owned by this project: {e}\")","preventionTips":["Always run env commands from the project root containing pyproject.toml.","Prefer removing envs by their listed name ('poetry env list') rather than by raw interpreter path.","Remember base_env_name is derived from cwd - moving the project directory invalidates old env names.","Never point env remove at a global or foreign project interpreter."],"tags":["env-remove","project-ownership","virtualenv","safety-guard"],"analyzedSha":"92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54","analyzedAt":"2026-08-04T20:33:34.072Z","schemaVersion":2}