Topic 4: Importing Modules and Using Libraries

1. Introduction

Modules in Python are simply files containing Python definitions, functions, and statements. Libraries, on the other hand, are collections of modules that provide functionalities to perform various tasks without writing code from scratch. Importing these modules or libraries allows users to leverage these pre-written codes.

2. Basic Module Import

To use a module in your program, it must first be imported.

a. import statement

Using the import keyword, you can load a module into your script.

python
import math print(math.sqrt(16)) # Outputs: 4.0
b. from...import statement

Allows you to import specific attributes or functions from a module.

python
from datetime import date print(date.today()) # Outputs current date
c. import...as statement

Allows you to provide a shorthand alias for the module.

python
import numpy as np array = np.array([1, 2, 3])

3. Exploring Standard Libraries

Python comes with a vast standard library, some of which include:

a. datetime

Provides functionalities to handle dates and time.

python
from datetime import datetime now = datetime.now() print(now.strftime('%Y-%m-%d %H:%M:%S'))
b. os

Provides a way of using operating system-dependent functionality.

python
import os print(os.getcwd()) # Outputs the current working directory
c. sys

Allows access to some variables used or maintained by the interpreter.

python
import sys for path in sys.path: print(path)

4. Exploring Third-Party Libraries

Beyond the standard library, there are thousands of third-party libraries. Some notable ones include:

a. numpy

A library for the Python programming language, adding support for large, multi-dimensional arrays and matrices.

python
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(np.mean(arr))
b. pandas

Offers data structures and operations for manipulating numerical tables and time series.

python
import pandas as pd data = {'Name': ['John', 'Anna'], 'Age': [28, 22]} df = pd.DataFrame(data) print(df)
c. requests

A simple HTTP library for Python.

python
import requests response = requests.get('https://www.example.com') print(response.text)

5. Installing Third-Party Libraries

While Python’s standard library comes pre-installed, third-party libraries typically need to be installed using package managers like pip.

bash
pip install requests

6. Creating Your Own Modules

You can create your own module by saving your code as a .py file. This file can then be imported into other scripts.

my_module.py

python
def my_function(): return "Hello from my module!"

main.py

python
import my_module print(my_module.my_function()) # Outputs: Hello from my module!

7. Conclusion

Modules and libraries are fundamental to Python development. They promote code reusability and help you build applications efficiently. Whether you’re using Python’s extensive standard library, tapping into the vast ecosystem of third-party packages, or creating your own modules, understanding how to import and utilize modules is a critical skill for every Python developer.