poetry 2 install command can not find pyproject.toml with --only and --directory parameters
Description
I have a pyproject.toml with package-mode = false and I'm copying it into a docker image. I installed the dependencies like this in previous versions of poetry:
poetry install --only main --no-cache -C /tmp
/tmp contains the pyproject.toml and poetry.lock files.
The command fails with poetry 2. Error message:
Poetry could not find a pyproject.toml file in / or its parents
The current directory during docker build is /. Poetry should change the directory to /tmp because of the -C /tmp parameter.
If I change the order of the parameters like this:
poetry install -C /tmp --only main --no-cache
then poetry successfully installs the dependencies.
Workarounds
Specify -C parameter before the --only parameter
Poetry Installation Method
pip
Operating System
Alpine 3.21
Poetry Version
2.0.0
Poetry Configuration
cache-dir = "/home/user/.cache/pypoetry"
installer.max-workers = null
installer.no-binary = null
installer.only-binary = null
installer.parallel = true
installer.re-resolve = true
keyring.enabled = true
requests.max-retries = 0
solver.lazy-wheel = true
system-git-client = false
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs" # /home/user/.cache/pypoetry/virtualenvs
virtualenvs.prompt = "{project_name}-py{python_version}"
virtualenvs.use-poetry-python = false
Python Sysconfig
No response
Example pyproject.toml
No response
Poetry Runtime Logs
Stack trace:
12 /opt/doc_venv/lib/python3.13/site-packages/cleo/application.py:327 in run
325│
326│ try:
→ 327│ exit_code = self._run(io)
328│ except BrokenPipeError:
329│ # If we are piped to another process, it may close early and send a
11 /opt/doc_venv/lib/python3.13/site-packages/poetry/console/application.py:236 in _run
234│
235│ with directory(self._working_directory):
→ 236│ exit_code: int = super()._run(io)
237│
238│ return exit_code
10 /opt/doc_venv/lib/python3.13/site-packages/cleo/application.py:431 in _run
429│ io.input.interactive(interactive)
430│
→ 431│ exit_code = self._run_command(command, io)
432│ self._running_command = None
433│
9 /opt/doc_venv/lib/python3.13/site-packages/cleo/application.py:473 in _run_command
471│
472│ if error is not None:
→ 473│ raise error
474│
475│ return terminate_event.exit_code
8 /opt/doc_venv/lib/python3.13/site-packages/cleo/application.py:454 in _run_command
452│
453│ try:
→ 454│ self._event_dispatcher.dispatch(command_event, COMMAND)
455│
456│ if command_event.command_should_run():
7 /opt/doc_venv/lib/python3.13/site-packages/cleo/events/event_dispatcher.py:26 in dispatch
24│
25│ if listeners:
→ 26│ self._do_dispatch(listeners, event_name, event)
27│
28│ return event
6 /opt/doc_venv/lib/python3.13/site-packages/cleo/events/event_dispatcher.py:85 in _do_dispatch
83│ break
84│
→ 85│ listener(event, event_name, self)
86│
87│ def _sort_listeners(self, event_name: str) -> None:
5 /opt/doc_venv/lib/python3.13/site-packages/poetry/console/application.py:343 in configure_env
341│
342│ io = event.io
→ 343│ poetry = command.poetry
344│
345│ env_manager = EnvManager(poetry, io=io)
4 /opt/doc_venv/lib/python3.13/site-packages/poetry/console/commands/command.py:24 in poetry
22│ def poetry(self) -> Poetry:
23│ if self._poetry is None:
→ 24│ return self.get_application().poetry
25│
26│ return self._poetry
3 /opt/doc_venv/lib/python3.13/site-packages/poetry/console/application.py:181 in poetry
179│ return self._poetry
180│
→ 181│ self._poetry = Factory().create_poetry(
182│ cwd=self._project_directory,
183│ io=self._io,
2 /opt/doc_venv/lib/python3.13/site-packages/poetry/factory.py:60 in create_poetry
58│ io = NullIO()
59│
→ 60│ base_poetry = super().create_poetry(cwd=cwd, with_groups=with_groups)
61│
62│ if version_str := base_poetry.local_config.get("requires-poetry"):
1 /opt/doc_venv/lib/python3.13/site-packages/poetry/core/factory.py:48 in create_poetry
46│ from poetry.core.pyproject.toml import PyProjectTOML
47│
→ 48│ poetry_file = self.locate(cwd)
49│ pyproject = PyProjectTOML(path=poetry_file)
50│
RuntimeError
Poetry could not find a pyproject.toml file in / or its parents
at /opt/doc_venv/lib/python3.13/site-packages/poetry/core/factory.py:802 in locate
798│ if poetry_file.exists():
799│ return poetry_file
800│
801│ else:
→ 802│ raise RuntimeError(
803│ f"Poetry could not find a pyproject.toml file in {cwd} or its parents"
804│ )
805│
Probably belongs in cleo, not here
The root cause of the issue might be cleo indeed, but it might be a "missing feature" for cleo or lack of proper way to register custom application level options.
CC: @Secrus for visibility.
For folks who want to test the fix out you can do so as described in https://github.com/python-poetry/poetry/pull/10021#issuecomment-2585476914.
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.