python-poetry/poetry · error · IncorrectEnvError
Env {env_name} doesn't belong to this project.
Error message
Env {env_name} doesn't belong to this project. What it means
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.
Source
Thrown at src/poetry/utils/env/env_manager.py:291
Check if env name starts with projects name.
This is done to prevent action on other project's envs.
"""
return env.startswith(base_env_name)
def remove(self, python: str) -> Env:
python_path = Path(python)
if python_path.is_file():
# Validate env name if provided env is a full path to python
try:
env_dir = subprocess.check_output(
[python, "-c", GET_ENV_PATH_ONELINER], text=True, encoding="locale"
).strip("\n")
env_name = Path(env_dir).name
if not self.check_env_is_for_current_project(
env_name, self.base_env_name
):
raise IncorrectEnvError(env_name)
except CalledProcessError as e:
raise EnvCommandError(e)
if self.check_env_is_for_current_project(python, self.base_env_name):
venvs = self.list()
for venv in venvs:
if venv.path.name == python:
# Exact virtualenv name
if self.envs_file.exists():
venv_minor = ".".join(str(v) for v in venv.version_info[:2])
self.envs_file.remove_section(self.base_env_name, venv_minor)
self.remove_venv(venv.path)
return venv
raise ValueError(
f'<warning>Environment "{python}" does not exist.</warning>'View on GitHub (pinned to 92b74dcfe3)
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.
Example fix
// before - path points into another project's venv
manager.remove("/home/u/other-project/.venv/bin/python") # IncorrectEnvError
// after - remove by the env name listed for THIS project
manager.remove("myproject-py3.11") Defensive patterns
Strategy: validation
Validate before calling
from pathlib import Path
def env_belongs_to_project(manager, python_path: str) -> bool:
p = Path(python_path)
if not p.is_file():
return False
import subprocess
try:
env_dir = subprocess.check_output(
[python_path, "-c", "import sys; print(sys.prefix)"], text=True
).strip()
except subprocess.CalledProcessError:
return False
return Path(env_dir).name.startswith(manager.base_env_name) Type guard
from poetry.utils.env.exceptions import IncorrectEnvError, EnvError
def is_incorrect_env(e: Exception) -> bool:
return isinstance(e, IncorrectEnvError) or (isinstance(e, EnvError) and "belong to this project" in str(e)) Try / catch
from poetry.utils.env.exceptions import IncorrectEnvError
try:
manager.remove(python_path)
except IncorrectEnvError as e:
raise SystemExit(f"Refusing to remove env not owned by this project: {e}") Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- <warning>Environment "{python}" does not exist.</warning>
- <warning>Environment "{name}" does not exist.</warning>
- Add the --all option if you want to clear all cache entries
- Discovered shell '{shell}' doesn't have an activator in virt
- embedded {distribution} wheel not found
AI-assisted analysis of python-poetry/poetry@92b74dcfe3 (2026-08-04).
Data as JSON: /data/errors/cc7530e4865aefec.json.
Report an issue: GitHub.