__Init__ file not working properly in python

I have several folders and .pyfiles in the following structure:

parent/
       __init__.py
       test.ipynb
       code/
            __init__.py
            common.py
            subcode/
                    __init__.py
                    get_data.py

In the file __init__in the folder parentI have import code, and in codeI have import subcode. But when I tried import code.subcode, I got this error:

ImportError: No module named 'code.subcode'; 'code' is not a package

But when I am simple import code, an error does not occur. However, when I call code.subcode, this error occurs:

AttributeError: module 'code' has no attribute 'subcode' 

I will try all of the above in test.ipynb, which is in the root of the directory.

Do you know what the reason is and how I can fix it? Thank!

+4
source share
2 answers

, code, , . , code.__file__ import code.

, , , code . - , , , , .

, , , , parent PYTHONPATH.

, , , ( . , ). , , , , - . , .

+4

Python 3. :

from . import code

code, , code.

+2

Source: https://habr.com/ru/post/1649481/


All Articles