Importing modules in Python and __init__.py

后端 未结 4 2122
臣服心动
臣服心动 2020-12-08 14:26

I have been reading about the function of __init__.py file. It is said that we need an empty __init__.py file in the folder which contains modules,

4条回答
  •  难免孤独
    2020-12-08 14:58

    Yes, this works, as you can tell. The reason for the empty __init__.py file is to mark sub-folders as folders containing modules.

    So a folder in PYTHONPATH is ok to have modules in it, but any subfolders of those folders have to have a __init__.py file in them to be able to import modules from them.

提交回复
热议问题