How Do You Get the libselinux-python to Work with Python 3?

Problem scenario
You have Python 2 and Python 3 installed. When you run Python 3 programs, you get an error message about selinux. The message is consistent with libselinux-python not being installed.

When you enter the Python interpreter for Python 2, you can run this command without errors: import selinux

But when you enter the Python interpreter for Python 3 (e.g., python3), you get errors when you run this command: import selinux

Security of the server is not critical, and you have no access to download packages from the internet. What should you do?

Solution

  1. Run this command from a terminal: python -v
  2. From the Python 2 interpreter line, run this command: import selinux
  3. The output of the above should show where selinux is. Enter this command to get back to a command prompt: quit()
  4. Find where Python 3's packages should be with this command: sudo find / -name site-packages | grep python3
  5. Run a command like this to create site-packages in a Python 3 directory if you found nothing from step #4.
  6. Run a modified version of this command: sudo cp -R /path/to/source /path/to/destination
    But replace /path/to/source with the site-packages directory found in step #2's results. Replace /path/to/destination with the site-packages directory found in step #4 or created in step #5.

Leave a comment

Your email address will not be published. Required fields are marked *