``` ❯ python -c 'import pathlib; import importlib; importlib.import_module(pathlib.Path("poc.py"))' Traceback (most recent call last): File "", line 1, in File "~/.conda/envs/py3.6/lib/python3.6/importlib/__init__.py", line 117, in import_module if name.startswith('.'): AttributeError: 'PosixPath' object has no attribute 'startswith' ```
Thanks for the report, but importlib.import_module() doesn't accept a path of a Python module: https://docs.python.org/3/library/importlib.html#importlib.import_module So, the correct way to use the API is: import importlib importlib.import_module('poc') Whether we should explicitly reject passing a file path to import_module() is up to the importlib maintainers.
I concur with Berker. Since the argument of importlib.import_module() is not a path, pathlib.Path is not valid here. It is uncommon in Python to check the type of arguments explicitly. pathlib.Path is not more special than list or tkinter.Button.