Skip to main content

模块 Modules

dog.py
def bark():
print("woof!")
main.py
import dog
from dog import bark

dog.bark() # "woof!"
bark() # "woof!"

Create lib folder

__init__.py (nothing here, just to delcare the lib is a module in python)

dog.py
def bark():
print("woof!")
main.py
from lib import dog
from lib.dog import bark

dog.bark() # "woof!"
bark() # "woof!"

Buit-in modules

  • math for math utilities
  • re for regular expressions
  • json to work with JSON datetime to work with dates sqlite to use SQLite
  • os for Operating System utilities
  • random for random number generation
  • statistics for statistics utilities requests to perform HTTP network requests
  • http to create HTTP servers
  • urllib to manage URLS
import math
from math import sqrt

math.sqrt(4) # 2.0
sqrt(4) # 2.0