python38/import_failed.py
Matej Cepl 0bcf65704a Accepting request 889802 from home:bnavigator:branches:devel:languages:python:Factory
- Make sure to close the import_failed.map file after the exception
  has been raised in order to avoid ResourceWarnings when the
  failing import is part of a try...except block.

Please sync to the other flavors.

This fixes test suite failures of packages with `-W error` and optional imports.
(e.g. pytest-doctestplus)

OBS-URL: https://build.opensuse.org/request/show/889802
OBS-URL: https://build.opensuse.org/package/show/devel:languages:python:Factory/python38?expand=0&rev=65
2021-05-02 17:07:37 +00:00

24 lines
706 B
Python

import sys, os
from sysconfig import get_path
failed_map_path = os.path.join(get_path('stdlib'), '_import_failed', 'import_failed.map')
if __spec__:
failed_name = __spec__.name
else:
failed_name = __name__
with open(failed_map_path) as fd:
for line in fd:
package = line.split(':')[0]
imports = line.split(':')[1]
if failed_name in imports:
raise ImportError(f"""Module '{failed_name}' is not installed.
Use:
sudo zypper install {package}
to install it.""")
raise ImportError(f"""Module '{failed_name}' is not installed.
It is supposed to be part of python3 distribution, but missing from failed import map.
Please file a bug on the SUSE Bugzilla.""")