Given the following directory structure:
here/
├── app
│ ├── __init__.py
│ ├── json.py
│ └── example.py
└── my_script.py
__init__.pyand json.py- empty files.
Content my_script.py:
from app import example
Content example.py
import importlib, imp, sys, os
sys.path = [p for p in sys.path if p]
os.environ.pop('PYTHONPATH', None)
assert not os.path.isfile('json.py')
print '1: ', imp.find_module('json')
print '2: ', __import__('json')
print '3: ', importlib.import_module('json')
import json
json.loads
Now from the directory heredo:
python ./my_script.py
You will see that methods 1, 2, 3, all find the version of the base module library json.
However, the actual import statement still manages to somehow capture the empty file json.py( AttributeError: 'module' object has no attribute 'loads').
I realized that the batch version of json here should be available only for the namespace, i.e. from app import jsonbut the namespace does not work here.
On python3, I cannot reproduce the problem. I also noticed that if we put from __future__ import absolute_importin a file example.py, the problem just disappears.
?
: , import json, json, sys.modules . , python , ?