Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weird behavior of the import statement #119086

Closed
plzfgme opened this issue May 16, 2024 · 1 comment
Closed

weird behavior of the import statement #119086

plzfgme opened this issue May 16, 2024 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@plzfgme
Copy link

plzfgme commented May 16, 2024

Bug report

Bug description:

File structure:

 a
├──  __init__.py
├──  c.py
└──  d.py

File content:

# __init__.py
b = "b"
from .c import c

__all__ = ["b", "c"]
# c.py
c = "c"
# d.py
d = "d"

Run in python:

Python 3.11.9 (main, Jan  1 1980, 00:00:00) [Clang 16.0.6 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import a.b as b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'a.b'
>>> import a.c as c
>>> c
'c'
>>> import a.d as d
>>> d
<module 'a.d' from '/the/path/to/Python-3.11.9/a/d.py'>

I think for b and d, it works correctly because b is not a module and d is. But for c, I expect it to be a module, but it is actually a variable.

I am not familiar to python but I cannot find any info about this behavior, so I ask it here.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

@plzfgme plzfgme added the type-bug An unexpected behavior, bug, or error label May 16, 2024
@serhiy-storchaka
Copy link
Member

This was discussed recently in #117860. It is not obvious that this is a bug. The code in __init__.py sets c to 'c'. import a.c as c is equivalent to import a.c; c = a.c.

To avoid confusion try to not override the names which correspond to names of submodules with other values.

@serhiy-storchaka serhiy-storchaka closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants