suchen
HeimBackend-EntwicklungPython-TutorialDetaillierte Erläuterung der Rolle der Datei __init__.py in Python

__init__.py 文件的作用是将文件夹变为一个Python模块,Python 中的每个模块的包中,都有__init__.py 文件。

通常__init__.py 文件为空,但是我们还可以为它增加其他的功能。我们在导入一个包时,实际上是导入了它的__init__.py文件。这样我们可以在__init__.py文件中批量导入我们所需要的模块,而不再需要一个一个的导入。

# package
# __init__.py
import re
import urllib
import sys
import os
# a.py
import package 
print(package.re, package.urllib, package.sys, package.os)

注意这里访问__init__.py文件中的引用文件,需要加上包名。

__init__.py中还有一个重要的变量,__all__, 它用来将模块全部导入。

# __init__.py
__all__ = ['os', 'sys', 're', 'urllib']
# a.py
from package import *

这时就会把注册在__init__.py文件中__all__列表中的模块和包导入到当前文件中来。

可以了解到,__init__.py主要控制包的导入行为。要想清楚理解__init__.py文件的作用,还需要详细了解一下import语句引用机制:

可以被import语句导入的对象是以下类型:

•模块文件(.py文件)

•C或C++扩展(已编译为共享库或DLL文件)

•包(包含多个模块)

•内建模块(使用C编写并已链接到Python解释器中)

当导入模块时,解释器按照sys.path列表中的目录顺序来查找导入文件。

import sys
>>> print(sys.path)
# Linux:
['', '/usr/local/lib/python3.4',
'/usr/local/lib/python3.4/plat-sunos5',
'/usr/local/lib/python3.4/lib-tk',
'/usr/local/lib/python3.4/lib-dynload',
'/usr/local/lib/python3.4/site-packages']
# Windows:
['', 'C:\\WINDOWS\\system32\\python34.zip', 'C:\\Documents and Settings\\weizhong', 'C:\\Python34\\DLLs', 'C:\\Python34\\lib', 'C:\\Python34\\lib\\plat-win', 'C:\\Python34\\lib\\lib-tk', 'C:\\Python34\\Lib\\site-packages\\pythonwin', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages', 'C:\\Python34\\lib\\site-packages\\win32', 'C:\\Python34\\lib\\site-packages\\win32\\lib', 'C:\\Python34\\lib\\site-packages\\wx-2.6-msw-unicode']

其中list第一个元素空字符串代表当前目录。

关于.pyc 文件 与 .pyo 文件

.py文件的汇编,只有在import语句执行时进行,当.py文件第一次被导入时,它会被汇编为字节代码,并将字节码写入同名的.pyc文件中。后来每次导入操作都会直接执行.pyc 文件(当.py文件的修改时间发生改变,这样会生成新的.pyc文件),在解释器使用-O选项时,将使用同名的.pyo文件,这个文件去掉了断言(assert)、断行号以及其他调试信息,体积更小,运行更快。(使用-OO选项,生成的.pyo文件会忽略文档信息)

导入模块

模块通常为单独的.py文件,可以用import直接引用,可以作为模块的文件类型有.py、.pyo、.pyc、.pyd、.so、.dll

在导入模块时,解释器做以下工作:

1.已导入模块的名称创建新的命名空间,通过该命名空间就可以访问导入模块的属性和方法。

2.在新创建的命名空间中执行源代码文件。

3.创建一个名为源代码文件的对象,该对象引用模块的名字空间,这样就可以通过这个对象访问模块中的函数及变量

import 语句可以在程序的任何位置使用,你可以在程序中多次导入同一个模块,但模块中的代码仅仅在该模块被首次导入时执行。后面的import语句只是简单的创建一个到模块名字空间的引用而已。

sys.modules字典中保存着所有被导入模块的模块名到模块对象的映射。

导入包

多个相关联的模块组成一个包,以便于维护和使用,同时能有限的避免命名空间的冲突。一般来说,包的结构可以是这样的:

package
|- subpackage1
|- __init__.py
|- a.py
|- subpackage2
|- __init__.py
|- b.py

有以下几种导入方式:

import subpackage1.a # 将模块subpackage.a导入全局命名空间,例如访问a中属性时用subpackage1.a.attr
from subpackage1 import a # 将模块a导入全局命名空间,例如访问a中属性时用a.attr_a
from subpackage.a import attr_a # 将模块a的属性直接导入到命名空间中,例如访问a中属性时直接用attr_a 
使用from语句可以把模块直接导入当前命名空间,from语句并不引用导入对象的命名空间,而是将被导入对象直接引入当前命名空间。

以上所述是小编给大家介绍的Python中__init__.py文件的作用详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHP中文网的支持!

更多Python中__init__.py文件的作用详解相关文章请关注PHP中文网!


Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Python: Ein tiefes Eintauchen in Zusammenstellung und InterpretationPython: Ein tiefes Eintauchen in Zusammenstellung und InterpretationMay 12, 2025 am 12:14 AM

PythonusesahybridmodelofCompilation und Interpretation: 1) thepythonInterPreterCompilessourceCodeIntoplatform-unintenpendentBytecode.2) Thepythonvirtualmachine (PVM) ThenexexexexecthisByTeCode, BalancingeAnsewusewithperformance.

Ist Python eine interpretierte oder eine kompilierte Sprache, und warum ist es wichtig?Ist Python eine interpretierte oder eine kompilierte Sprache, und warum ist es wichtig?May 12, 2025 am 12:09 AM

Pythonisbothinterpreted und kompiliert.1) ItscompiledToByteCodeForPortabilityAcrossplatform.2) thytecodeTheninterpreted, und das ErlaubnisfordyNamictyPingandRapidDevelopment zulässt, obwohl es sich

Für Schleife vs während der Schleife in Python: Schlüsselunterschiede erklärtFür Schleife vs während der Schleife in Python: Schlüsselunterschiede erklärtMay 12, 2025 am 12:08 AM

ForloopsaridealWenyouKnowtHenumberofofiterationssinadvance, während whileloopsarebetterForsituationswhereyouneedtoloopuntilaconditionismet.forloopsaremoreffictionAndable, geeigneter Verfaserungsverlust, whereaswiloopsofofermorcontrolanduseusefulfulf

Für und während Schleifen: ein praktischer LeitfadenFür und während Schleifen: ein praktischer LeitfadenMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

Python: Ist es wirklich interpretiert? Die Mythen entlarvenPython: Ist es wirklich interpretiert? Die Mythen entlarvenMay 12, 2025 am 12:05 AM

Pythonisnotpurelyinterpretiert; itusesahybridapproachofByteCodecompilation undruntimeinterpretation.1) PythoncompilessourcecodeIntoBytecode, die ISthenexecutBythepythonvirtualmachine (Pvm)

Python -Verkettungslisten mit demselben ElementPython -Verkettungslisten mit demselben ElementMay 11, 2025 am 12:08 AM

ToconcatenatelistsinpythonWithThesameElements, Verwendung: 1) Die Operatortokeepduplikate, 2) asettoremoveduplicate, or3) listenConpRectionforControloverDuplikate, EvermethodhasDifferentPerformanceInDormplocate.

Interpretiert gegen kompilierte Sprachen: Pythons PlatzInterpretiert gegen kompilierte Sprachen: Pythons PlatzMay 11, 2025 am 12:07 AM

PythonisaninterpretedLuage, OfferingaseofuseandflexibilitätsbutfacingPerformancelimitationsincriticalApplications.1) InterpretedLanguages ​​LikePythonexecutine-by-Line, ermöglicht, dassmediateFeedbackandrapidPrototyping.2) CompiledLanguagesslikec/C.5.

Für und während der Schleifen: Wann benutzt du jeweils in Python?Für und während der Schleifen: Wann benutzt du jeweils in Python?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofofiterationssisknowninadvance und wileloopswhenCiterationsDependonacondition.1) Forloopsardealforsequencelistorranges.2) Während

See all articles

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Nordhold: Fusionssystem, erklärt
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Flüstern des Hexenbaum
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

SublimeText3 Linux neue Version

SublimeText3 Linux neue Version

SublimeText3 Linux neueste Version

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Leistungsstarke integrierte PHP-Entwicklungsumgebung

EditPlus chinesische Crack-Version

EditPlus chinesische Crack-Version

Geringe Größe, Syntaxhervorhebung, unterstützt keine Code-Eingabeaufforderungsfunktion

SAP NetWeaver Server-Adapter für Eclipse

SAP NetWeaver Server-Adapter für Eclipse

Integrieren Sie Eclipse mit dem SAP NetWeaver-Anwendungsserver.

MantisBT

MantisBT

Mantis ist ein einfach zu implementierendes webbasiertes Tool zur Fehlerverfolgung, das die Fehlerverfolgung von Produkten unterstützen soll. Es erfordert PHP, MySQL und einen Webserver. Schauen Sie sich unsere Demo- und Hosting-Services an.