Fix `get_class_in_module` (#21709)
Fix get_class_in_module Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
This commit is contained in:
parent
ed6ceb7649
commit
4c6346cc3e
|
@ -154,7 +154,17 @@ def get_class_in_module(class_name, module_path):
|
|||
shutil.copy(f"{module_dir}/{module_file_name}", tmp_dir)
|
||||
# On Windows, we need this character `r` before the path argument of `os.remove`
|
||||
cmd = f'import os; os.remove(r"{module_dir}{os.path.sep}{module_file_name}")'
|
||||
subprocess.run(["python", "-c", cmd])
|
||||
# We don't know which python binary file exists in an environment. For example, if `python3` exists but not
|
||||
# `python`, the call `subprocess.run(["python", ...])` gives `FileNotFoundError` (about python binary). Notice
|
||||
# that, if the file to be removed is not found, we also have `FileNotFoundError`, but it is not raised to the
|
||||
# caller's process.
|
||||
try:
|
||||
subprocess.run(["python", "-c", cmd])
|
||||
except FileNotFoundError:
|
||||
try:
|
||||
subprocess.run(["python3", "-c", cmd])
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
# copy back the file that we want to import
|
||||
shutil.copyfile(f"{tmp_dir}/{module_file_name}", f"{module_dir}/{module_file_name}")
|
||||
|
|
Loading…
Reference in New Issue