classification
Title: imp.find_module("") and imp.find_module(".")
Type: Stage: resolved
Components: Documentation, Interpreter Core Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Arfrever, BTaskaya, docs@python, ezio.melotti, ncoghlan
Priority: normal Keywords:

Created on 2011-09-25 20:49 by Arfrever, last changed 2020-10-22 19:51 by iritkatriel. This issue is now closed.

Messages (3)
msg144531 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2011-09-25 20:49
It's undocumented that imp.find_module("") and imp.find_module(".") try to find __init__.py.

There is also a small difference in behavior between them.
sys.path by default contains "" as the first element, which is sufficient for imp.find_module("."), but not for imp.find_module(""):

$ mkdir /tmp/imp_tests
$ cd /tmp/imp_tests
$ touch __init__.py
$ python3.3 -c 'import imp, sys; print(repr(sys.path[0])); print(imp.find_module("."))'
''
(None, '.', ('', '', 5))
$ python3.3 -c 'import imp, sys; print(repr(sys.path[0])); print(imp.find_module(""))'
''
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named ''

If sys.path contains path (e.g. "." or absolute path) to directory with __init__.py file, then imp.find_module("") will succeed:

$ PYTHONPATH="." python3.3 -c 'import imp, sys; print(repr(sys.path[0:2])); print(imp.find_module("."))'
['', '/tmp/imp_tests']
(None, '.', ('', '', 5))
$ PYTHONPATH="." python3.3 -c 'import imp, sys; print(repr(sys.path[0:2])); print(imp.find_module(""))'
['', '/tmp/imp_tests']
(None, '/tmp/imp_tests/', ('', '', 5))
$ python3.3 -c 'import imp, sys; sys.path.insert(1, "."); print(repr(sys.path[0:2])); print(imp.find_module("."))'
['', '.']
(None, '.', ('', '', 5))
$ python3.3 -c 'import imp, sys; sys.path.insert(1, "."); print(repr(sys.path[0:2])); print(imp.find_module(""))'
['', '.']
(None, './', ('', '', 5))

I think that imp.find_module(".") and imp.find_module("") should have the same behavior, and this behavior should be documented.
msg144545 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2011-09-26 18:04
I don't think this is undocumented as much as it's unexpected behavior. I really doubt this functionality was  on purpose.
msg358760 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python triager) Date: 2019-12-21 06:40
`imp` is now deprecated and IMHO no reason to make this change after this deprecation.
History
Date User Action Args
2020-10-22 19:51:44iritkatrielsetstatus: open -> closed
resolution: out of date
stage: resolved
2019-12-21 06:40:19BTaskayasetnosy: + BTaskaya
messages: + msg358760
2013-01-25 19:34:04brett.cannonsetnosy: - brett.cannon
2011-09-26 18:04:41brett.cannonsetmessages: + msg144545
2011-09-25 20:54:09ezio.melottisetnosy: + brett.cannon, ncoghlan, ezio.melotti
2011-09-25 20:49:57Arfrevercreate