Issue 36056: importlib does not support pathlib

Issue36056

Created on 2019-02-20 19:02 by majuscule, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg336136 - (view) Author: Dylan Lloyd (majuscule) Date: 2019-02-20 19:02
```
❯ python -c 'import pathlib; import importlib; importlib.import_module(pathlib.Path("poc.py"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  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'
```
msg336137 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-02-20 19:08
This is also present in 3.7 and 3.8. I would look at it and propose a patch tomorrow.
msg336138 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2019-02-20 19:13
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.
msg336139 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-02-20 19:31
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.
msg336141 - (view) Author: Dylan Lloyd (majuscule) Date: 2019-02-20 19:41
Whoops, yes, that of course makes sense. My mistake. Thanks!
msg336143 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-02-20 19:52
I close this issue, it's not a bug.
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80237
2019-02-20 21:01:42berker.peksagsettype: enhancement
resolution: not a bug
2019-02-20 19:52:28matrixisesetstatus: open -> closed

messages: + msg336143
nosy: + matrixise

2019-02-20 19:41:00majusculesetstatus: closed -> open
type: enhancement -> (no value)
resolution: not a bug -> (no value)
messages: + msg336141
2019-02-20 19:31:44serhiy.storchakasetstatus: open -> closed

messages: + msg336139
nosy: + serhiy.storchaka

2019-02-20 19:14:06berker.peksagsetstatus: pending -> open
nosy: + remi.lapeyre
2019-02-20 19:13:46berker.peksagsetstatus: open -> pending

type: enhancement
versions: - Python 3.7, Python 3.8
nosy: + berker.peksag, brett.cannon, - remi.lapeyre

messages: + msg336138
resolution: not a bug
stage: resolved

2019-02-20 19:08:54remi.lapeyresetnosy: + remi.lapeyre

messages: + msg336137
versions: + Python 3.7, Python 3.8

2019-02-20 19:02:21majusculecreate