Skip to content

Fix interpreter selection in the ansible-cmdb wrapper - #4

Open
gdevenyi wants to merge 2 commits into
chenri2006:masterfrom
DouglasNeuroInformatics:fix/python3-binary-lookup
Open

Fix interpreter selection in the ansible-cmdb wrapper#4
gdevenyi wants to merge 2 commits into
chenri2006:masterfrom
DouglasNeuroInformatics:fix/python3-binary-lookup

Conversation

@gdevenyi

@gdevenyi gdevenyi commented Aug 5, 2026

Copy link
Copy Markdown

Two independent ways the src/ansible-cmdb wrapper picks the wrong interpreter — or none at all. Both are one-liners in find_py_bin().

1. No unversioned python on modern distros

Fixes fboender/ansible-cmdb#240, and as a side effect #189.

The search was:

which -a python | while read -r TRY_PY_BIN

Debian 11+ and recent Ubuntu ship no unversioned python unless python-is-python3 is installed, so this finds nothing and the wrapper aborts:

No suitable python version found (v2.7 or higher required). Aborting

...on a system with a perfectly good Python 3. Now searches python3 python2 python.

This also resolves fboender#189. The loop accepts the first candidate that is Python 3 or Python ≥2.7, so where /usr/bin/python is 2.7 it won that race and you got missing-dependency failures. Listing python3 first means it is tried first.

2. Installed venv binaries fail unless the venv is activated

Because the search only consults $PATH, an installed wrapper invoked by absolute path finds the system python:

$ .venv/bin/ansible-cmdb --version
ModuleNotFoundError: No module named 'ansiblecmdb'

That's easy to misdiagnose, because activating the venv makes it disappear.

The wrapper now prefers a python3/python sitting next to itself. In a virtualenv that's the interpreter the package was installed for, and the only one that can import ansiblecmdb. With no sibling — system-wide install, or running from the source tree — it falls through to the $PATH search unchanged.

Testing

  • Reproduced Fix python binary lookup on debian bullseye (11) fboender/ansible-cmdb#240 with an isolated PATH holding python3 but no bare python: old code finds nothing, new code resolves python3.
  • Installed into a venv and ran <venv>/bin/ansible-cmdb --version by absolute path with the venv not activated: fails before, works after.
  • Source-tree invocation (no sibling interpreter) still resolves via $PATH.
  • sh -n clean.

No conflicts with the other open PRs — this branch only touches src/ansible-cmdb.

The src/ansible-cmdb wrapper searched for an interpreter with
'which -a python'. On Debian 11 (bullseye) and later, and on recent
Ubuntu, there is no unversioned 'python' binary unless python-is-python3
is installed, so the search returned nothing and the wrapper aborted with:

    No suitable python version found (v2.7 or higher required). Aborting

Search 'python3 python2 python' instead. The existing version check in
the loop already prefers Python 3, and listing python3 first means it is
found without relying on a distro-provided compatibility symlink.

Original patch by oxivanisher in fboender#240.
Copilot AI lite review requested due to automatic review settings August 5, 2026 18:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the src/ansible-cmdb shell wrapper’s interpreter discovery so it works on modern Debian/Ubuntu systems that don’t provide an unversioned python binary by default, preventing false “No suitable python version found” aborts.

Changes:

  • Searches for python3, then python2, then python when locating a usable interpreter (while retaining the existing version-selection logic).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

The wrapper searches $PATH for an interpreter, so an installed
'<venv>/bin/ansible-cmdb' invoked by its absolute path finds the *system*
python rather than the venv's, and dies before it does anything useful:

    $ .venv/bin/ansible-cmdb --version
    ModuleNotFoundError: No module named 'ansiblecmdb'

It only works when the venv happens to be activated, which is easy to
miss because activating it makes the problem disappear.

Check for a python3/python sitting next to the wrapper first. In a
virtualenv that is the interpreter the package was installed for, and
the only one that can import ansiblecmdb. When there is no sibling --
a system-wide install, or running from the source tree -- it falls
through to the existing $PATH search, so those paths are unchanged.
@gdevenyi gdevenyi changed the title Look for python3/python2 binaries, not just 'python', in wrapper Fix interpreter selection in the ansible-cmdb wrapper Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

prefering python2 causes failure because of missing dependancies

2 participants