This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Import precedence is broken in some library
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, jinseo.kim, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-08-12 16:43 by jinseo.kim, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg375262 - (view) Author: Jinseo Kim (jinseo.kim) Date: 2020-08-12 16:43
I found this behavior by chance:

  >>> with open('tokenize.py', 'w') as f:
  ...   f.write("print('This is so sad')")
  ...
  >>> import linecache
  This is so sad
  >>>

  path/of/python/Lib/linecache.py:
    import functools
    import sys
    import os
    import tokenize

    [...]

Meanwhile,

  >>> with open('functools.py', 'w') as f:
  ...     f.write("print('This is so sad')")
  ...
  >>> import linecache
  >>>

It seems for me to be broken: 'import' doesn't have clear precedence.
msg375263 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-08-12 16:51
Did you restart python before the functools test?
msg375264 - (view) Author: Jinseo Kim (jinseo.kim) Date: 2020-08-12 16:58
Yes, I restarted and cleared directory before each test.
msg375265 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-08-12 17:06
What is your environment - system and python version?

I get this on master checkout on windows 10:

Running Release|Win32 interpreter...
Python 3.10.0a0 (heads/master:46e19b61d3, Aug 12 2020, 18:02:36) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('functools.py', 'w') as f:
...      f.write("print('This is so sad')")
...
23
>>> import linecache
This is so sad
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\src\cpython\lib\linecache.py", line 11, in <module>
    import tokenize
  File "C:\Users\User\src\cpython\lib\tokenize.py", line 32, in <module>
    import re
  File "C:\Users\User\src\cpython\lib\re.py", line 315, in <module>
    @functools.lru_cache(_MAXCACHE)
AttributeError: module 'functools' has no attribute 'lru_cache'
>>> quit()
C:\Users\User\src\cpython>del functools.py

C:\Users\User\src\cpython>python
Running Release|Win32 interpreter...
Python 3.10.0a0 (heads/master:46e19b61d3, Aug 12 2020, 18:02:36) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('tokenize.py', 'w') as f:
...      f.write("print('This is so sad')")
...
23
>>> import linecache
This is so sad
>>>
msg375266 - (view) Author: Jinseo Kim (jinseo.kim) Date: 2020-08-12 17:09
My environment is Ubuntu 18.04.4

Python version:
  Python 3.8.0 (default, Oct 28 2019, 16:14:01)
  [GCC 8.3.0] on linux
msg375268 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-08-12 17:28
It is because module functools is already imported (you can see it in sys.modules), but module tokenize is not. It is likely due to module rlcompleter imported at startup on Linux, but not on Windows.
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85704
2020-08-12 17:28:52serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg375268

resolution: not a bug
stage: resolved
2020-08-12 17:09:40jinseo.kimsetmessages: + msg375266
2020-08-12 17:06:08iritkatrielsetmessages: + msg375265
2020-08-12 16:58:04jinseo.kimsetmessages: + msg375264
2020-08-12 16:51:34iritkatrielsetnosy: + iritkatriel
messages: + msg375263
2020-08-12 16:43:17jinseo.kimcreate