Modules & Pip

A module is a file containing Python definitions and statements.Pip is the package installer for Python.

1. Importing Modules

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

from os import path
print(path.exists("app.py"))

2. Using Pip

Install libraries from the Python Package Index (PyPI).

# Install a package
pip install requests

# List installed packages
pip list

# Export dependencies
pip freeze > requirements.txt

# Install from file
pip install -r requirements.txt

3. Creating Your Own Module

Just save code in my_module.py and import it in another file.

# my_module.py
def say_hi():
    print("Hi!")

# main.py
import my_module
my_module.say_hi()
Best Practice: Always use pip list or pip freezeto keep track of your project's dependencies for others to replicate your environment.