最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Python中import機制

 更新時間:2020年09月11日 09:00:14   作者:Python學(xué)習(xí)者  
這篇文章主要介紹了Python中import機制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下

Python語言中import的使用很簡單,直接使用import module_name語句導(dǎo)入即可。這里我主要寫一下"import"的本質(zhì)。

Python官方定義:

Python code in one module gains access to the code in another module by the process of importing it.

1.定義:

  • 模塊(module):用來從邏輯(實現(xiàn)一個功能)上組織Python代碼(變量、函數(shù)、類),本質(zhì)就是*.py文件。文件是物理上組織方式"module_name.py",模塊是邏輯上組織方式"module_name"。
  • 包(package):定義了一個由模塊和子包組成的Python應(yīng)用程序執(zhí)行環(huán)境,本質(zhì)就是一個有層次的文件目錄結(jié)構(gòu)(必須帶有一個__init__.py文件)。

2.導(dǎo)入方法

# 導(dǎo)入一個模塊
import model_name
# 導(dǎo)入多個模塊
import module_name1,module_name2
# 導(dǎo)入模塊中的指定的屬性、方法(不加括號)、類
from moudule_name import moudule_element [as new_name]

方法使用別名時,使用"new_name()"調(diào)用函數(shù),文件中可以再定義"module_element()"函數(shù)。

3.import本質(zhì)(路徑搜索和搜索路徑)

  • moudel_name.py
# -*- coding:utf-8 -*-
print("This is module_name.py")

name = 'Hello'

def hello():
 print("Hello")
  • module_test01.py
# -*- coding:utf-8 -*-
import module_name

print("This is module_test01.py")
print(type(module_name))
print(module_name)

運行結(jié)果:

E:\PythonImport>python module_test01.py
This is module_name.py
This is module_test01.py
<class 'module'>
<module 'module_name' from 'E:\\PythonImport\\module_name.py'>

在導(dǎo)入模塊的時候,模塊所在文件夾會自動生成一個__pycache__\module_name.cpython-35.pyc文件。

"import module_name" 的本質(zhì)是將"module_name.py"中的全部代碼加載到內(nèi)存并賦值給與模塊同名的變量寫在當(dāng)前文件中,這個變量的類型是'module';<module 'module_name' from 'E:\\PythonImport\\module_name.py'>

  • module_test02.py
# -*- coding:utf-8 -*-
from module_name import name

print(name)

運行結(jié)果;

E:\PythonImport>python module_test02.py
This is module_name.py
Hello

"from module_name import name" 的本質(zhì)是導(dǎo)入指定的變量或方法到當(dāng)前文件中。

  • package_name / __init__.py
# -*- coding:utf-8 -*-

print("This is package_name.__init__.py")
  • module_test03.py
# -*- coding:utf-8 -*-
import package_name

print("This is module_test03.py")

運行結(jié)果:

E:\PythonImport>python module_test03.py
This is package_name.__init__.py
This is module_test03.py

"import package_name"導(dǎo)入包的本質(zhì)就是執(zhí)行該包下的__init__.py文件,在執(zhí)行文件后,會在"package_name"目錄下生成一個"__pycache__ / __init__.cpython-35.pyc" 文件。

  • package_name / hello.py
# -*- coding:utf-8 -*-

print("Hello World")
  • package_name / __init__.py
# -*- coding:utf-8 -*-
# __init__.py文件導(dǎo)入"package_name"中的"hello"模塊
from . import hello
print("This is package_name.__init__.py")

運行結(jié)果:

E:\PythonImport>python module_test03.py
Hello World
This is package_name.__init__.py
This is module_test03.py

在模塊導(dǎo)入的時候,默認(rèn)現(xiàn)在當(dāng)前目錄下查找,然后再在系統(tǒng)中查找。系統(tǒng)查找的范圍是:sys.path下的所有路徑,按順序查找。

4.導(dǎo)入優(yōu)化

  • module_test04.py
# -*- coding:utf-8 -*-
import module_name 

def a():
 module_name.hello()
 print("fun a")

def b():
 module_name.hello()
 print("fun b")

a()
b()

運行結(jié)果:

E:\PythonImport>python module_test04.py
This is module_name.py
Hello
fun a
Hello
fun b

多個函數(shù)需要重復(fù)調(diào)用同一個模塊的同一個方法,每次調(diào)用需要重復(fù)查找模塊。所以可以做以下優(yōu)化:

  • module_test05.py
# -*- coding:utf-8 -*-
from module_name import hello 

def a():
 hello()
 print("fun a")

def b():
 hello()
 print("fun b")

a()
b()

運行結(jié)果:

E:\PythonImport>python module_test04.py
This is module_name.py
Hello
fun a
Hello
fun b

可以使用"from module_name import hello"進(jìn)行優(yōu)化,減少了查找的過程。

5.模塊的分類

內(nèi)建模塊

可以通過 "dir(__builtins__)" 查看Python中的內(nèi)建函數(shù)

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__','__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round','set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

非內(nèi)建函數(shù)需要使用"import"導(dǎo)入。Python中的模塊文件在"安裝路徑\Python\Python35\Lib"目錄下。

第三方模塊

通過"pip install "命令安裝的模塊,以及自己在網(wǎng)站上下載的模塊。一般第三方模塊在"安裝路徑\Python\Python35\Lib\site-packages"目錄下。

以上就是詳解Python中import機制的詳細(xì)內(nèi)容,更多關(guān)于Python import機制的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python面向?qū)ο缶幊蘲epr方法示例詳解

    Python面向?qū)ο缶幊蘲epr方法示例詳解

    這篇文章主要介紹了Python面向?qū)ο缶幊蘲epr方法的示例詳解,文中附含詳細(xì)的代碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • 詳解解決Python memory error的問題(四種解決方案)

    詳解解決Python memory error的問題(四種解決方案)

    這篇文章主要介紹了詳解解決Python memory error的問題(四種解決方案),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python畫圖小案例之小雪人超詳細(xì)源碼注釋

    Python畫圖小案例之小雪人超詳細(xì)源碼注釋

    在看了很多Python教程之后,覺得是時候做點什么小項目來練練手了,于是想來想去,用python寫了一個小雪人,代碼注釋無比詳細(xì)清楚,快來看看吧
    2021-09-09
  • numpy中軸處理的實現(xiàn)

    numpy中軸處理的實現(xiàn)

    本文主要介紹了numpy中軸處理的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Python3 shelve對象持久存儲原理詳解

    Python3 shelve對象持久存儲原理詳解

    這篇文章主要介紹了Python3 shelve對象持久存儲原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Python內(nèi)存管理器如何實現(xiàn)池化技術(shù)

    Python內(nèi)存管理器如何實現(xiàn)池化技術(shù)

    Python中的內(nèi)存管理是從三個方面來進(jìn)行的,一對象的引用計數(shù)機制,二垃圾回收機制,三內(nèi)存池機制,下面這篇文章主要給大家介紹了關(guān)于Python內(nèi)存管理器如何實現(xiàn)池化技術(shù)的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • 基于Python實現(xiàn)一個圖片壓縮工具

    基于Python實現(xiàn)一個圖片壓縮工具

    圖片壓縮是在保持圖像質(zhì)量的同時減小圖像文件大小的過程,本文將學(xué)習(xí)如何使用Python來實現(xiàn)一個簡單但功能強大的圖片壓縮工具,以及如何在不同情境下進(jìn)行圖片壓縮,希望對大家有所幫助
    2024-01-01
  • 使用python將mysql數(shù)據(jù)庫的數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù)的方法

    使用python將mysql數(shù)據(jù)庫的數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù)的方法

    這篇文章主要介紹了使用python將mysql數(shù)據(jù)庫的數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • selenium+python實現(xiàn)1688網(wǎng)站驗證碼圖片的截取功能

    selenium+python實現(xiàn)1688網(wǎng)站驗證碼圖片的截取功能

    這篇文章主要介紹了selenium+python實現(xiàn)1688網(wǎng)站驗證碼圖片的截取,需要的朋友可以參考下
    2018-08-08
  • Django實現(xiàn)CAS+OAuth2的方法示例

    Django實現(xiàn)CAS+OAuth2的方法示例

    這篇文章主要介紹了Django實現(xiàn)CAS+OAuth2的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10

最新評論

滦平县| 财经| 边坝县| 泰来县| 嘉善县| 博乐市| 互助| 东方市| 吉木萨尔县| 尼玛县| 平谷区| 尼玛县| 邵武市| 南涧| 和平县| 怀宁县| 拜泉县| 英山县| 永登县| 奉新县| 扶风县| 庐江县| 庐江县| 吉首市| 黄大仙区| 天峻县| 呼玛县| 普宁市| 呼和浩特市| 武平县| 福清市| 盐津县| 读书| 义马市| 武定县| 塘沽区| 日照市| 遂平县| 大邑县| 秦安县| 新密市|