Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ใช้ Import หลายโมดูลใน Statement เดียว

import module1, module2, module3 คั่นด้วย Comma

ตัวอย่าง

>>> import math, sys, os

มี Alias

>>> import math as Mathematics, sys as System

Dynamic Import จาก List

>>> modnames = ["os", "sys", "math"]
>>> for lib in modnames:
...     globals()[lib] = __import__(lib)
หมายเหตุ: PEP 8 แนะนำ Import แยกบรรทัด สำหรับ Dynamic Import ใช้ importlib.import_module() แทน __import__()