I have some folders and .py files in the following structure:
parent/
__init__.py
test.ipynb
code/
__init__.py
common.py
subcode/
__init__.py
get_data.py
In the __init__ file under the parent folder, I have import code and in the one of code, I have import subcode. But when I tried import code.subcode, I got such an error:
ImportError: No module named 'code.subcode'; 'code' is not a package
But when I just import code, no error is thrown. However, when I call code.subcode, this error happens:
AttributeError: module 'code' has no attribute 'subcode'
I try all of those mentioned above in the test.ipynb, which is at the root of the directory.
Do you know what is the reason and how can I fix it? Thanks!
__init__.pyfiles empty.codedoescode.__file__point to the directory that you expect?'/home/lcc/anaconda3/envs/parent/lib/python3.5/code.py'. Why does it happen...PYTHONPATHenvironment variable does not contain yourparentdirectory and if it does, then it is placed lower down in the list than the path to thecodemodule that you are actually importing. Solution: addparentto yourPYTHONPATHand maybe use a different name thancodeto avoid namespace collisions.