Modules, Packages, pip & venv

মডিউল, প্যাকেজ, pip ও venv — কোডকে ফাইলের বাইরে নিয়ে যাওয়া

Read: ~30 min Intermediate 5 practice problems Live code runner

1. Why Modules Exist

A serious program is never a single file. You break it up into small, focused files called modules, and you group related modules together into packages. Python has a first-class import system — every standard library tool, every third-party library from PyPI, and every file you write yourself uses the same mechanism. Today you will learn how import really works, how to write your own module, and how to use pip and venv to manage dependencies safely.

একটি বাস্তব প্রোগ্রাম কখনও একটিমাত্র ফাইলে লেখা হয় না। আপনি কোডকে ছোট ছোট ফোকাসড ফাইলে ভাগ করেন — এগুলোকে বলা হয় module, এবং সম্পর্কিত module-গুলো একত্রে রাখলে হয় package। Python-এর import system first-class — standard library, PyPI-এর library এবং আপনার নিজের ফাইল, সবকিছুর জন্য একই মেকানিজম ব্যবহার হয়।

2. How import Works

When you write import math, Python searches sys.path for a file or package named math, compiles it to bytecode, executes it once, and binds the resulting module object to the name math in your current namespace.

import math লিখলে Python প্রথমে sys.path-এ math নামে একটি ফাইল বা প্যাকেজ খোঁজে, তারপর সেটি bytecode-এ compile করে, একবার execute করে, এবং ফলাফল module object-কে আপনার namespace-এ math নামে bind করে।
import something — how Python finds it 1. sys.modules cache 2. Built-ins(math, sys, os) 3. Your script folder(current directory) 4. site-packages(pip-installed) Not found → ModuleNotFoundError Found → execute module, cache in sys.modules, bind name Figure 21.1 — Python-এর import resolution order।
import_demo.py
import math
from math import sqrt, pi
import math as m

print(math.factorial(5))   # 120
print(sqrt(144), pi)      # 12.0  3.14159...
print(m.gcd(24, 36))        # 12

3. Writing Your Own Module

Any .py file is a module. Put reusable functions in it, then import it from anywhere in the same folder. The special variable __name__ lets a file act both as a library and as a standalone script.

যেকোনো .py ফাইল নিজেই একটি module। সেখানে reusable function রাখুন, তারপর একই ফোল্ডারের যেকোনো জায়গা থেকে import করুন। বিশেষ variable __name__ ব্যবহার করে একটি ফাইলকে একই সাথে library এবং standalone script হিসেবে চালানো যায়।
mymath.py (single-file demo)
# This file demonstrates a "module" pattern in one runnable script.

def square(x):
    return x * x

def cube(x):
    return x ** 3

# The __name__ trick: runs only when executed directly.
if __name__ == "__main__":
    print("square(6) =", square(6))
    print("cube(4)   =", cube(4))

4. Packages — Folders That Python Understands

A package is simply a folder containing an __init__.py file plus any number of modules and sub-packages. A typical project might look like:

একটি package হলো এমন একটি ফোল্ডার, যার ভেতরে __init__.py ফাইল আছে এবং পাশাপাশি যেকোনো সংখ্যক module ও sub-package থাকতে পারে।
project layout
# myapp/
#   __init__.py        ← makes myapp a package
#   cli.py
#   utils/
#       __init__.py
#       text.py
#       numbers.py

# From another file you can then do:
# from myapp.utils.text import slugify
print("Package layout sketched above.")

5. pip and venv — Using the World's Code

PyPI (Python Package Index) hosts over 500,000 packages. You install from it with pip. Always install into an isolated virtual environment so projects do not fight over package versions.

PyPI-তে ৫ লক্ষেরও বেশি package আছে। pip দিয়ে সেগুলো install করা যায়। প্রতিটি project-এর জন্য একটি আলাদা virtual environment তৈরি করুন — এতে একাধিক project-এর মধ্যে package-এর version নিয়ে সংঘর্ষ এড়ানো যায়।
CommandWhat it doesবাংলায়
python -m venv .venvCreates a new virtual env in the folder .venv..venv নামে একটি নতুন virtual environment তৈরি করে।
.venv\Scripts\activate (Win) / source .venv/bin/activate (Unix)Activates the venv for your shell.বর্তমান shell-এ venv activate করে।
pip install requestsInstalls the requests package from PyPI.PyPI থেকে requests প্যাকেজ install করে।
pip freeze > requirements.txtSaves the exact versions you installed.বর্তমান সকল package ও version requirements.txt-এ save করে।
pip install -r requirements.txtRe-installs everything from the file.ফাইল থেকে সব package পুনরায় install করে।
deactivateLeaves the venv.venv থেকে বের হয়ে আসে।
Golden rule — একটি নতুন project শুরু করার প্রথম কাজ হলো python -m venv .venv। System-wide pip install এড়িয়ে চলুন।

6. Vocabulary

TermMeaningবাংলায়
ModuleA single .py file.একটি .py ফাইল।
PackageFolder with __init__.py grouping modules.__init__.py সমেত ফোল্ডার।
PyPIPython Package Index — the global public registry.Python-এর public package registry।
pipPython's package installer.Python-এর package installer।
venvIsolated per-project Python environment.প্রতি project-এর জন্য আলাদা Python environment।
sys.pathOrdered list of folders searched during import.Import-এর সময় যেসব ফোল্ডারে search হয়।

7. Practice Problems

Try each one first, then click Show Answer.

প্রতিটি প্রশ্ন প্রথমে নিজে চেষ্টা করুন, তারপর উত্তর দেখুন।
  1. Import random and print a random integer between 1 and 100.
    random import করে ১ থেকে ১০০-এর মধ্যে একটি random integer প্রিন্ট করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans1.py
    import random
    print(random.randint(1, 100))
  2. Use from math import sqrt, pi and compute the area and circumference of a circle of radius 5.
    from math import sqrt, pi ব্যবহার করে ৫ ব্যাসার্ধের একটি বৃত্তের area ও circumference বের করুন।
    ✨ Show Answer
    ans2.py
    from math import sqrt, pi
    r = 5
    print(f"area = {pi * r * r:.4f}")
    print(f"circ = {2 * pi * r:.4f}")
    print(f"sqrt(r) = {sqrt(r):.4f}")
  3. Why is from module import * considered bad practice?
    from module import * কেন খারাপ অভ্যাস?
    ✨ Show Answer

    Answer: It pollutes your namespace with every public name from the module, can silently shadow your own variables, makes it unclear where a name came from, and defeats tools like linters and IDE auto-complete. Prefer explicit imports: from module import thing_i_need.

    এটি namespace-কে module-এর সব public নাম দিয়ে ভরিয়ে দেয়, নীরবে আপনার variable overwrite করতে পারে, কোন নাম কোথা থেকে এসেছে বোঝা কঠিন হয় এবং linter/IDE-র auto-complete-ও ঠিকমতো কাজ করে না। তাই explicit import-ই ভালো।

  4. Write a small "module-style" file with a function greet(name) and a __main__ guard that calls it.
    একটি ছোট module-style ফাইল লিখুন যেখানে greet(name) function আছে এবং __main__ guard সেটিকে call করে।
    ✨ Show Answer
    ans4.py
    def greet(name):
        return f"Hello, {name}!"
    
    if __name__ == "__main__":
        print(greet("ABCL TECH"))
  5. Explain the difference between pip install foo and pip install -r requirements.txt.
    pip install foo এবং pip install -r requirements.txt-এর মধ্যে পার্থক্য ব্যাখ্যা করুন।
    ✨ Show Answer

    Answer: pip install foo installs a single package (latest compatible version). pip install -r requirements.txt reads a list of packages — often with pinned versions — from a file, installing exactly that set. The second form is what you use to reproduce a known-good environment on another machine or on CI.

    প্রথম command-টি একটি-মাত্র package install করে (latest version)। দ্বিতীয়টি একটি ফাইল থেকে pin-করা version সহ পুরো তালিকা পড়ে একই environment অন্য machine বা CI-তে পুনরায় তৈরি করে।

Summary — Module 21

A module is any .py file; a package is a folder with __init__.py. Python's import system searches sys.path, caches loaded modules, and executes each one exactly once. pip installs from PyPI, and venv isolates each project's dependencies. Master these and you can use — or write — any Python library in the world.

Module হলো .py ফাইল, package হলো __init__.py-সমেত ফোল্ডার। Python-এর import system sys.path-এ খোঁজে, cache রাখে এবং প্রতিটি module-কে একবারই execute করে। pip PyPI থেকে package install করে এবং venv প্রতিটি project-কে আলাদা রাখে।

Next Module → Classes & Objects — Python OOP।