新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中import語句為什么不執(zhí)行
Python語言中import的使用很簡單,直接使用import module_name語句導入即可。這里我主要寫一下"import"的本質。

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ù)、類),本質就是*.py文件。文件是物理上組織方式"module_name.py",模塊是邏輯上組織方式"module_name"。
包(package):定義了一個由模塊和子包組成的Python應用程序執(zhí)行環(huán)境,本質就是一個有層次的文件目錄結構(必須帶有一個__init__.py文件)。
2.導入方法
# 導入一個模塊 import model_name # 導入多個模塊 import module_name1,module_name2 # 導入模塊中的指定的屬性、方法(不加括號)、類 from moudule_name import moudule_element [as new_name]
方法使用別名時,使用"new_name()"調用函數(shù),文件中可以再定義"module_element()"函數(shù)。
3.import本質(路徑搜索和搜索路徑)
moudel_name.py
print("This is module_name.py")
name = 'Hello'
def hello():
print("Hello")module_test01.py
import module_name
print("This is module_test01.py")
print(type(module_name))
print(module_name)
運行結果: E:\PythonImport>python module_test01.py This is module_name.py This is module_test01.py
新聞名稱:創(chuàng)新互聯(lián)Python教程:python中import語句為什么不執(zhí)行
URL分享:http://m.5511xx.com/article/dphhohs.html


咨詢
建站咨詢
