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

python
import math print(math.sqrt(16)) # 4.0

Import specific names:

python
from math import sqrt, pi print(sqrt(16))

Import with alias:

python
import numpy as np

Your own modules

Create utils.py with functions. In another file in the same folder:

python
import 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.