Modules, Packages, pip & venv
মডিউল, প্যাকেজ, pip ও venv — কোডকে ফাইলের বাইরে নিয়ে যাওয়া
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.
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 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 হিসেবে চালানো যায়।
# 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:
__init__.py ফাইল আছে এবং পাশাপাশি যেকোনো সংখ্যক module ও sub-package থাকতে পারে।
# 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.
pip দিয়ে সেগুলো install করা যায়। প্রতিটি project-এর জন্য একটি আলাদা virtual environment তৈরি করুন — এতে একাধিক project-এর মধ্যে package-এর version নিয়ে সংঘর্ষ এড়ানো যায়।
| Command | What it does | বাংলায় |
|---|---|---|
python -m venv .venv | Creates 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 requests | Installs the requests package from PyPI. | PyPI থেকে requests প্যাকেজ install করে। |
pip freeze > requirements.txt | Saves the exact versions you installed. | বর্তমান সকল package ও version requirements.txt-এ save করে। |
pip install -r requirements.txt | Re-installs everything from the file. | ফাইল থেকে সব package পুনরায় install করে। |
deactivate | Leaves the venv. | venv থেকে বের হয়ে আসে। |
python -m venv .venv। System-wide pip install এড়িয়ে চলুন।
6. Vocabulary
| Term | Meaning | বাংলায় |
|---|---|---|
| Module | A single .py file. | একটি .py ফাইল। |
| Package | Folder with __init__.py grouping modules. | __init__.py সমেত ফোল্ডার। |
| PyPI | Python Package Index — the global public registry. | Python-এর public package registry। |
| pip | Python's package installer. | Python-এর package installer। |
| venv | Isolated per-project Python environment. | প্রতি project-এর জন্য আলাদা Python environment। |
sys.path | Ordered list of folders searched during import. | Import-এর সময় যেসব ফোল্ডারে search হয়। |
7. Practice Problems
Try each one first, then click Show Answer.
-
Import
randomand print a random integer between 1 and 100.randomimport করে ১ থেকে ১০০-এর মধ্যে একটি random integer প্রিন্ট করুন।✨ Show Answer (উত্তর দেখুন)
ans1.pyimport random print(random.randint(1, 100)) -
Use
from math import sqrt, piand compute the area and circumference of a circle of radius 5.from math import sqrt, piব্যবহার করে ৫ ব্যাসার্ধের একটি বৃত্তের area ও circumference বের করুন।✨ Show Answer
ans2.pyfrom 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}") -
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-ই ভালো।
-
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.pydef greet(name): return f"Hello, {name}!" if __name__ == "__main__": print(greet("ABCL TECH")) -
Explain the difference between
pip install fooandpip install -r requirements.txt.pip install fooএবংpip install -r requirements.txt-এর মধ্যে পার্থক্য ব্যাখ্যা করুন।✨ Show Answer
Answer:
pip install fooinstalls a single package (latest compatible version).pip install -r requirements.txtreads 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.
.py ফাইল, package হলো __init__.py-সমেত ফোল্ডার। Python-এর import system sys.path-এ খোঁজে, cache রাখে এবং প্রতিটি module-কে একবারই execute করে। pip PyPI থেকে package install করে এবং venv প্রতিটি project-কে আলাদা রাখে।