Reputation: 79
I am using Python 3.10.
os.py
in a directory, my_dir
.os.py
using import os
, from my_dir
directory.os
module, not mine.my_dir
in sys.path
as a first element, but still it's loading Python's os
module.datetime
. But it's working as expected. i.e., it's loading my datetime.py
, not Python's.Why does only the os
module have this problem? Is it a bug in Python 3?
Upvotes: 1
Views: 540
Reputation: 8510
if you want to import some module A that happens to have the name of some other module B, that its placement in the import hierarchy mean that module B is found first, then you need to use a relative import to clearly differentiate between the two from withing modules in the same package
for example, said you have
my_dir
__init__.py
os.py
app.py
for app.py to use your os.py you do
from .os import X
Upvotes: 1