Importing modules in Python and __init__.py

后端 未结 4 2121
臣服心动
臣服心动 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 15:13

    If a directory (folder) contains a __init__.py file then it becomes a package. What you thought you read was not strictly correct, as you found. A package can be imported as if it was a module by itself, and any code in __init__.py is run, although it is often empty. Packages are a way of grouping multiple modules together, and you can load them using:

    import package-name.module-name
    

    Packages can also be nested, and often are. Look in the Lib directory under your Python software directory for many examples.

提交回复
热议问题