Fundamentals 6 min read

Python Modules & Imports: Master Third‑Party Packages in 5 Minutes

This guide teaches you what Python modules are, demonstrates three import styles, shows how to install third‑party packages using reliable mirrors, and walks through practical examples—including random score generation and common pitfalls—to help you quickly leverage modules for automation and web scraping.

liandk
liandk
liandk
Python Modules & Imports: Master Third‑Party Packages in 5 Minutes

Core Goal

Master module import methods, install third‑party packages, avoid mirror errors, and unlock automation or web‑scraping capabilities within five minutes.

What is a module?

A module is pre‑written code packaged for reuse. Built‑in modules come with Python, while third‑party modules must be installed.

Three import styles

# Import the whole module
import random
print(random.randint(1, 10))

# Import specific functions
from random import randint, choice
print(randint(1, 5))
print(choice(["apple", "banana", "orange"]))

# Import with alias
import math as m
print(m.sqrt(36))

Installing third‑party modules

Use pip install package_name. When the default mirrors (Aliyun, Douban) fail, switch to Tsinghua or USTC mirrors:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
# or
pip install -i https://pypi.mirrors.ustc.edu.cn/simple requests

Comprehensive example

Generate random scores, classify them, and print results:

import random

def get_grade(score):
    if score >= 90:
        return "优秀"
    elif score >= 80:
        return "良好"
    elif score >= 60:
        return "及格"
    else:
        return "不及格"

def generate_random_scores(n):
    scores = []
    for i in range(n):
        score = random.randint(60, 100)
        scores.append(score)
    for score in scores:
        grade = get_grade(score)
        print(f"成绩:{score},等级:{grade}")

generate_random_scores(5)

Common pitfalls and solutions

ModuleNotFoundError – install the module using a reliable mirror.

Mirror parsing errors – switch to Tsinghua or USTC mirror URLs.

Forgot to prefix function with module name – use either module.function() or from module import function.

Misspelled module name – verify exact spelling.

Post‑lesson task

# 1. Compute 10² and 2³ with math
import math
print(math.pow(10, 2))
print(math.pow(2, 3))

# 2. Install requests via Tsinghua mirror
# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

# 3. Generate three random numbers between 1 and 20
import random
nums = [random.randint(1, 20) for _ in range(3)]
print(nums)

Key takeaways

Modules are ready‑made toolkits; built‑in modules are ready to use, third‑party modules require installation from reliable mirrors.

Three import patterns: whole module, specific functions, and alias.

When errors occur, first check installation, mirror source, and spelling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAutomationModulesimportpipMirror repositoriesThird‑party packages
liandk
Written by

liandk

Seasoned Java and mobile developer with years of experience, specializing in mini‑programs, public accounts, and full‑stack front‑end development. In the AI era, I continuously learn to broaden my knowledge and evolve. I revived a public account I started a decade ago during a dessert‑startup venture, using code as a vessel and knowledge as a companion. I share personal projects, technical articles, programming tips, and growth insights—let’s improve together and set sail.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.