Modules and Imports
Modules and Imports
A Python file is a module. Use import to use code from another file or the standard library.
Importing modules
pythonimport math print(math.sqrt(16)) # 4.0
Import specific names:
pythonfrom math import sqrt, pi print(sqrt(16))
Import with alias:
pythonimport numpy as np
Your own modules
Create utils.py with functions. In another file in the same folder:
pythonimport utils utils.some_function() # or from utils import some_function
Standard library
Python ships with many modules: os, sys, json, datetime, collections, itertools, etc. Use them to avoid reinventing the wheel.