python模塊是一個py檔案,一個模塊只會被匯入一次
python在編譯或安裝的時候會確定搜索路徑,使用import陳述句的時候,python解釋器就從搜索路徑(即一系列目錄名)中查找模塊
import sys print(sys.argv) # 命令列引數 print(sys.path) # 路徑
結果為:
['D:/Pycharm/pythonProject/main.py'] ['D:\\Pycharm\\pythonProject', 'D:\\Pycharm\\pythonProject', 'C:\\Program Files\\PerkinElmerInformatics\\ChemOffice2020\\ChemScript\\Lib', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\DLLs', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39', 'D:\\Pycharm\\pythonProject\\venv', 'D:\\Pycharm\\pythonProject\\venv\\lib\\site-packages', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32\\lib', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\Pythonwin']
import
from … import *
可以使用模塊名稱.函式名來訪問函式
例如我的myFunction.py中定義了max()函式,那么可以通過myFunction.max()來使用max()函式
__name__屬性
每個模塊都有一個__name__屬性,當其值是'__main__'時,表明該模塊自身在運行:
if __name__ == '__main__': print('程式自身在運行') else: print('我來自另一模塊')
如果程式自身運行,會輸出'程式自身在運行',如果被匯入其他模塊,會輸出'我來自另一模塊'
dir()函式可以找到模塊內定義的所有名稱,以一個字串串列的形式回傳:
print(dir()) # 不加引數,羅列出當前定義的所有名稱 a = 1 print(dir()) import sys print(dir(sys)) print(dir())
結果為:
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a'] ['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '__unraisablehook__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_home', '_xoptions', 'addaudithook', 'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'getallocatedblocks', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'platlibdir', 'prefix', 'pycache_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'unraisablehook', 'version', 'version_info', 'warnoptions', 'winver'] ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'sys']
python本身帶有一些標準的模塊庫,有些模塊直接被構建在決議器里,模塊 sys ,它內置在每一個 Python 決議器中
包是一種管理 Python 模塊命名空間的形式,本質上就是一個檔案夾,目錄只有包含一個叫做 __init__.py 的檔案才會被認作是一個包
匯入包中的模塊的方式和上面一樣
注意如果使用from package import *時,會把__init__.py中的一個叫做__all__的串列變數中的所有名字匯入(例如:__all__ = ['echo', 'surround', 'reverse']),如果__all__沒有定義,那么就不會匯入任何子模塊,而是匯入package中定義的所有內容
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/459508.html
標籤:Python
下一篇:python中類的匯入類
