Binaries in /var/home/$USER/bin won't run within Distrobox

I’ve broken something, basically no Python commands work in Distrobox and I don’t know how to recover. So for example, Ansible works fine in bluefin-dx:

phil@slug ~ 
❯ ansible --version
ansible [core 2.16.5]
  config file = None
  configured module search path = ['/var/home/phil/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /var/home/phil/.local/lib/python3.12/site-packages/ansible
  ansible collection location = /var/home/phil/.ansible/collections:/usr/share/ansible/collections
  executable location = /var/home/phil/.local/bin/ansible
  python version = 3.12.2 (main, Feb 21 2024, 00:00:00) [GCC 14.0.1 20240217 (Red Hat 14.0.1-0)] (/usr/bin/python)
  jinja version = 3.1.3
  libyaml = True

But if I create a new distrobox and enter it, Ansible fails. Running pip fails, etc:

phil@slug ~ 
❯ distrobox-create debian --name debian; distrobox-enter debian
Trying to pull ghcr.io/ublue-os/ubuntu-toolbox:latest...
Getting image source signatures
Checking if image destination supports signatures
Copying blob eb84c4a06bea done   | 
Copying blob 2c82088be606 done   | 
Copying blob 00414d36669e done   | 
Copying config a287ec2dc2 done   | 
Writing manifest to image destination
Storing signatures
a287ec2dc2abcb9ea268bf16d08a9375930e57b34747b65e022c0712b43b5118
Creating 'debian' using image ghcr.io/ublue-os/ubuntu-toolbox:latest	 [ OK ]
Distrobox 'debian' successfully created.
To enter, run:

distrobox enter debian

Starting container...                   	 [ OK ]
Installing basic packages...            	 [ OK ]
Setting up devpts mounts...             	 [ OK ]
Setting up read-only mounts...          	 [ OK ]
Setting up read-write mounts...         	 [ OK ]
Setting up host's sockets integration...	 [ OK ]
Integrating host's themes, icons, fonts...	 [ OK ]
Setting up package manager exceptions...	 [ OK ]
Setting up package manager hooks...     	 [ OK ]
Setting up dpkg exceptions...           	 [ OK ]
Setting up apt hooks...                 	 [ OK ]
Setting up distrobox profile...         	 [ OK ]
Setting up sudo...                      	 [ OK ]
Setting up user groups...               	 [ OK ]
Setting up kerberos integration...      	 [ OK ]
Setting up user's group list...         	 [ OK ]
Setting up existing user...             	 [ OK ]
Setting up user home...                 	 [ OK ]
Ensuring user's access...               	 [ OK ]

Container Setup Complete!
📦[phil@debian ~]$ ansible
bash: /var/home/phil/.local/bin/ansible: /usr/bin/python: bad interpreter: No such file or directory
📦[phil@debian ~]$ pip install ansible
bash: /var/home/phil/.local/bin/pip: /usr/bin/python: bad interpreter: No such file or directory

But following the error, I do see Ansible, and pip, at the path it says it can’t find it in:

📦[phil@debian ~]$ ls -al /var/home/phil/.local/bin/ansible
-rwxr-xr-x. 1 phil phil 215 Apr 10 15:19 /var/home/phil/.local/bin/ansible
📦[phil@debian ~]$ ls -al /var/home/phil/.local/bin/pip
-rwxr-xr-x. 1 phil phil 220 Mar 26 10:42 /var/home/phil/.local/bin/pip

Lastly, I’m currently running the dx:40:

● ostree-image-signed:docker://ghcr.io/ublue-os/bluefin-dx:40
                   Digest: sha256:e97161aef35b1beacb613da970a8383c4af107357ebf738b5421f274da55e05b
                  Version: 40.20240423.0 (2024-04-24T14:53:09Z)

Not sure how I got to this state or what to reset or fix. Ideas? Thank you!

Hi @philcryer ,

Don’t worry. Nothing is broken. The python interpreter on Debian does not reside at /usr/bin/python but at /usr/bin/python3 because it was really important during the python 2 to python 3 transition to be explicit about which python version you want to use. The transition was long and painful, because people just kept writing scripts for the python interpreter instead for the python3 interpreter.

So basically, you need to slightly adjust your ansible script to use the python3 interpreter on all systems. This is best practise anyway and it will work on any modern Linux.

1 Like

Okay, this makes sense, but just changing python to python3 gives a new error… maybe I need to change other files it relies on?

So again, we see it’s broken when run it in Distrobox:

📦[phil@debian ~]$ ansible
bash: /var/home/phil/.local/bin/ansible: /usr/bin/python: bad interpreter: No such file or directory

Let’s rewrite #!/usr/bin/python to #!/usr/bin/python3:

📦[phil@debian ~]$ grep -rl "!/usr/bin/python" ~/.local/bin/ansible | xargs sed -i 's/\!\/usr\/bin\/python/\!\/usr\/bin\/python3/g'

It looks right:

📦[phil@debian ~]$ cat ~/.local/bin/ansible
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from ansible.cli.adhoc import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

And we can verify it’s the right path for python:

📦[phil@debian ~]$ ls -al /usr/bin/python3
lrwxrwxrwx. 1 root root 10 Aug 18  2022 /usr/bin/python3 -> python3.10

But now when I try to run it I get a new error:

📦[phil@debian ~]$ ansible
Traceback (most recent call last):
  File "/var/home/phil/.local/bin/ansible", line 5, in <module>
    from ansible.cli.adhoc import main
ModuleNotFoundError: No module named 'ansible'

That’s a fairly old notation to explicitly declare the encoding of a python script. I suspect that your ansible installation expects python 2 instead of python 3. How did you install ansible?

edit: I looked up the newest version of ansible, which is 9.5.1. But according to your shell-dump above you are on version [core 2.16.5] which seems to be very outdated.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.