current position:Home>About creating in Python project folder__ init__. Understanding of PY

About creating in Python project folder__ init__. Understanding of PY

2022-05-15 05:58:28__ Watson__

stay Python In order to facilitate the management of code logic in the project , Usually the py file ( Pay attention to Python One of them py The file is called a Module) Archive in the appropriate folder .

1. No, __init__.py

At this point, the folder is just a path , Need to import py file (Module) In the properties of the 、 function 、 Class needs the following access methods :

from 【 Folder .py file name 】 import 【 Properties in this module 、 function 、 class 】

2. Yes __init__.py

When creating in a folder __init__.py File can make the folder into a Module,
When this folder (Module) By import when , Will execute __init__.py Code in ,
__ init__.py This folder is usually written in it Module The children in the garden Module Specific needs import Properties of , function , Class etc. ,__ init__.py The contents are as follows :

from 【. Son Module file name 】import Son Module In the properties of the , function , Class etc.

In this way, when other files access the module, the following access forms can be realized :

from 【 Folder Module】 import 【 Son Module In the properties of the , function , Class etc.

Be careful init.py In general, you can write __all__ = [...] Used to correct import External exposure control , Or not , No restriction is made by default

copyright notice
author[__ Watson__],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110609597630.html

Random recommended