python-poetry / python-poetry/poetry

Poetry commands fail if python 2.7 is in path

Open
#10,897 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

kind/bug status/triage
Dominant language
Python
Stars
34.3k
Forks
2.5k
Avg merge
2d 19h
Merged PRs (30d)
30

Description

Description

With Poetry 2.4 (but not 2.3), various commands fail when Python 2.7 is within the path with the following error:

Command '['/usr/bin/python', '-Ic', 'import sys; print(sys.executable)']' returned non-zero exit status 2.

For example:

$ /usr/bin/python3.12 -m poetry init

This command will guide you through creating your pyproject.toml config.

Package name [foo]:
Version [0.1.0]:
Description []:
Author [None, n to skip]:  n
License []:

Command '['/usr/bin/python', '-Ic', 'import sys; print(sys.executable)']' returned non-zero exit status 2.

or

# with existing pyproject.toml
$ /usr/bin/python3.12 -m poetry install

Command '['/usr/bin/python', '-Ic', 'import sys; print(sys.executable)']' returned non-zero exit status 2.

or

# with existing pyproject.toml
$ /usr/bin/python3.12 -m poetry env use python3.12

Command '['/usr/bin/python', '-Ic', 'import sys; print(sys.executable)']' returned non-zero exit status 2.
Workarounds

Couldn't find one so far. Removing python 2.7 from the $PATH is unfortunately not an option as diverse system utilities depend on it (unfortunately, we are currently stuck with a somewhat outdated OS).

Poetry Installation Method

pip

Operating System

CentOS 7

Poetry Version

Poetry (version 2.4.1)

Poetry Configuration
cache-dir = "/root/.cache/pypoetry"
data-dir = "/root/.local/share/pypoetry"
installer.max-workers = null
installer.no-binary = null
installer.only-binary = null
installer.parallel = true
installer.re-resolve = false
keyring.enabled = true
python.installation-dir = "{data-dir}/python"  # /root/.local/share/pypoetry/python
requests.max-retries = 0
solver.lazy-wheel = true
solver.min-release-age = 0
solver.min-release-age-exclude = null
solver.min-release-age-exclude-source = null
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"  # /root/.cache/pypoetry/virtualenvs
virtualenvs.prompt = "{project_name}-py{python_version}"
virtualenvs.use-poetry-python = false
Python Sysconfig

I think this is irrelevant for the issue, but I can provide it on request.

Example pyproject.toml
irrelevant
Poetry Runtime Logs
poetry-runtime.log
/usr/bin/python3.12 -m poetry -vvv init

This command will guide you through creating your pyproject.toml config.

Package name [foo]:
Version [0.1.0]:
Description []:
Author [None, n to skip]:  n
License []:
Loading configuration file /root/.config/pypoetry/config.toml
[findpython:findpython] Running script: ['/usr/bin/python', '-Ic', 'import sys; print(sys.executable)']

Stack trace:

14  /usr/lib/python3.12/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

13  /usr/lib/python3.12/site-packages/poetry/console/application.py:260 in _run
     258│
     259│             try:
   → 260│                 exit_code = super()._run(io)
     261│             except PoetryRuntimeError as e:
     262│                 io.write_error_line("")

12  /usr/lib/python3.12/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│

11  /usr/lib/python3.12/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

10  /usr/lib/python3.12/site-packages/cleo/application.py:457 in _run_command
     455│
     456│             if command_event.command_should_run():
   → 457│                 exit_code = command.run(io)
     458│             else:
     459│                 exit_code = ConsoleCommandEvent.RETURN_CODE_DISABLED

 9  /usr/lib/python3.12/site-packages/cleo/commands/base_command.py:117 in run
     115│         io.input.validate()
     116│
   → 117│         return self.execute(io) or 0
     118│
     119│     def merge_application_definition(self, merge_args: bool = True) -> None:

 8  /usr/lib/python3.12/site-packages/cleo/commands/command.py:61 in execute
      59│
      60│         try:
   →  61│             return self.handle()
      62│         except KeyboardInterrupt:
      63│             return 1

 7  /usr/lib/python3.12/site-packages/poetry/console/commands/init.py:84 in handle
      82│                 return 1
      83│
   →  84│         return self._init_pyproject(project_path=project_path)
      85│
      86│     def _init_pyproject(

 6  /usr/lib/python3.12/site-packages/poetry/console/commands/init.py:176 in _init_pyproject
     174│             python = (
     175│                 ">="
   → 176│                 + Python.get_preferred_python(config, self.io).minor_version.to_string()
     177│             )
     178│

 5  /usr/lib/python3.12/site-packages/poetry/utils/env/python/manager.py:290 in get_preferred_python
     288│         ):
     289│             io.write_error_line(
   → 290│                 f"Found: {active_python.executable}", verbosity=Verbosity.VERBOSE
     291│             )
     292│             return active_python

 4  /usr/lib/python3.12/site-packages/poetry/utils/env/python/manager.py:180 in executable
     178│     @property
     179│     def executable(self) -> Path:
   → 180│         return cast("Path", self._python.interpreter)
     181│
     182│     @property

 3  /usr/lib/python3.12/site-packages/findpython/python.py:81 in interpreter
      79│     def interpreter(self) -> Path:
      80│         if self._interpreter is None:
   →  81│             self._interpreter = Path(self._get_interpreter())
      82│         return self._interpreter
      83│

 2  /usr/lib/python3.12/site-packages/findpython/python.py:237 in _get_interpreter
     235│     def _get_interpreter(self) -> str:
     236│         script = "import sys; print(sys.executable)"
   → 237│         return _run_script(str(self.executable), script).strip()
     238│
     239│     def _get_freethreaded(self) -> bool:

 1  /usr/lib/python3.12/site-packages/findpython/python.py:23 in _run_script
      21│     command = [executable, "-Ic", script]
      22│     logger.debug("Running script: %s", command)
   →  23│     return subprocess.run(
      24│         command,
      25│         stdout=subprocess.PIPE,

CalledProcessError

Command '['/usr/bin/python', '-Ic', 'import sys; print(sys.executable)']' returned non-zero exit status 2.

at /usr/lib64/python3.12/subprocess.py:571 in run
     567│             # We don't call process.wait() as .__exit__ does that for us.
     568│             raise
     569│         retcode = process.poll()
     570│         if check and retcode:
  →  571│             raise CalledProcessError(retcode, process.args,
     572│                                      output=stdout, stderr=stderr)
     573│     return CompletedProcess(process.args, retcode, stdout, stderr)
     574│
     575│

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with poetry/utils/env/python/manager.py around get_preferred_python, then inspect findpython/python.py at _get_interpreter and _run_script. Reproduce with Python 2.7 earlier in PATH using the reported init, install, or env use commands; done means these commands work without the CalledProcessError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.