Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom python install breaks ansible and molecule -- get_system_sitepackages() #69

Open
br-olf opened this issue Mar 14, 2023 · 2 comments

Comments

@br-olf
Copy link

br-olf commented Mar 14, 2023

Hello, this issue is related to #58 and #17

I encountered an error when my systems python version upgraded (to 3.11) and I still need an older one (3.10) for molecule. So i installed it from source.

But all ansible commands broke with the following error:

Traceback (most recent call last):
  File "/path/to/my/venv/bin/ansible-config", line 5, in <module>
    from ansible.cli.config import main
  File "/path/to/my/venv/lib/python3.10/site-packages/ansible/cli/__init__.py", line 101, in <module>
    from ansible.module_utils.common.file import is_executable
  File "/path/to/my/venv/lib/python3.10/site-packages/ansible/module_utils/common/file.py", line 25, in <module>
    import selinux
  File "/path/to/my/venv/lib/python3.10/site-packages/selinux/__init__.py", line 106, in <module>
    check_system_sitepackages()
  File "/path/to/my/venv/lib/python3.10/site-packages/selinux/__init__.py", line 95, in check_system_sitepackages
    system_sitepackages = get_system_sitepackages()
  File "/path/to/my/venv/lib/python3.10/site-packages/selinux/__init__.py", line 81, in get_system_sitepackages
    subprocess.check_output(
  File "/usr/local/lib/python3.10/subprocess.py", line 421, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/usr/local/lib/python3.10/subprocess.py", line 503, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/local/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.10/subprocess.py", line 1847, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/bin/python3.10'

After tedious hours of bug hunting I discovered the following code in selinux/__init__.py:

    def get_system_sitepackages():
        """Get sitepackage locations from system python"""
        # Do not ever use sys.executable here
        # See https://github.com/pycontribs/selinux/issues/17 for details
        system_python = "/usr/bin/python%s" % ".".join(
            [str(item) for item in platform.python_version_tuple()[0:2]]
        )

        system_sitepackages = json.loads(
            subprocess.check_output(
                [
                    system_python,
                    "-c",
                    "import json, site; print(json.dumps(site.getsitepackages()))",
                ]
            ).decode("utf-8")
        )
        return system_sitepackages

That code assumes that any installed python is always located under /usr/bin which is problematic.

I don't know what was the reason for not using sys.executable but after seeing the original issue was related to python2 I think it will work nowadays.

I made a pull request and didn't found any issues in my tests: #68

@joshtrutwin
Copy link

+1

cannot use pyenv etc - thanks!

@minsis
Copy link

minsis commented Nov 20, 2023

This issue is also problematic for conda environments.

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

No branches or pull requests

3 participants