[Test] Workaround for importlib crazy behavior

- I can't call pip to install in a specified dir and then import
  using importlib. It always fail, even when the module is installed
  and its path is in sys.path. Couldn't find why.
This commit is contained in:
Salvador E. Tropea 2022-07-08 10:57:03 -03:00
parent d6aa9b2446
commit 120220b986
2 changed files with 11 additions and 6 deletions

View File

@ -235,10 +235,12 @@ def check_pip():
def pip_install(pip_command, dest=None, name='.'): def pip_install(pip_command, dest=None, name='.'):
cmd = [pip_command, 'install', '-U', '--no-warn-script-location', cmd = [pip_command, 'install', '-U', '--no-warn-script-location']
# This is what -U (--user) means, but Debian's pip installs to /usr/local when used by root if name == '.':
'--root', os.path.dirname(site.USER_BASE), '--prefix', os.path.basename(site.USER_BASE), # Applied only when installing a downloaded tarball
name] # This is what -U (--user) means, but Debian's pip installs to /usr/local when used by root
cmd.extend(['--root', os.path.dirname(site.USER_BASE), '--prefix', os.path.basename(site.USER_BASE)])
cmd.append(name)
logger.debug('- Running: {}'.format(cmd)) logger.debug('- Running: {}'.format(cmd))
try: try:
res_run = subprocess.run(cmd, check=True, capture_output=True, cwd=dest) res_run = subprocess.run(cmd, check=True, capture_output=True, cwd=dest)
@ -708,9 +710,10 @@ def check_tool_python(dep, reload=False):
return None return None
# Check we can use it # Check we can use it
try: try:
importlib.invalidate_caches()
mod = importlib.import_module(dep.module_name) mod = importlib.import_module(dep.module_name)
res = check_tool_python_version(mod, dep) res = check_tool_python_version(mod, dep)
if res is not None and reload is not None: if res is not None and reload:
res = importlib.reload(reload) res = importlib.reload(reload)
return res return res
except ModuleNotFoundError: except ModuleNotFoundError:

View File

@ -49,6 +49,8 @@ def try_dependency(ctx, caplog, monkeypatch, docstring, name_dep, downloader_nam
def try_dependency_module(ctx, caplog, monkeypatch, docstring, name_dep, downloader_name): def try_dependency_module(ctx, caplog, monkeypatch, docstring, name_dep, downloader_name):
# Note: every attempt to install in a chosen dir failed, even when the module was there and in the sys.path the
# importlib call miserably failed.
with monkeypatch.context(): with monkeypatch.context():
# Refresh the module with actual dependencies # Refresh the module with actual dependencies
mod = importlib.reload(downloader) mod = importlib.reload(downloader)
@ -60,7 +62,7 @@ def try_dependency_module(ctx, caplog, monkeypatch, docstring, name_dep, downloa
cov.start() cov.start()
# Python module # Python module
downloader_func = getattr(mod, downloader_name) downloader_func = getattr(mod, downloader_name)
res = downloader_func(dep, False) res = downloader_func(dep)
cov.stop() cov.stop()
cov.save() cov.save()
# We should get the following name: # We should get the following name: