Functions, Methods, Modules, and Libraries in Python
Here, I have consolidated all the points related to Functions, Methods, Modules and Library in Python. Hope you will find it useful.
Type |
Description |
Examples |
Functions |
Standalone blocks of code inside a def . |
math.sqrt() , os.path.exists() , json.loads() |
Methods |
Functions within a class, always linked to objects. |
list.append() , str.upper() , dict.items() |
Modules |
A Python file with a .py extension containing functions, classes, and other code. |
import math
from ikea import add |
Libraries |
A collection of modules. Libraries contain similar modules for similar tasks. |
import pandas as pd |
Importing Modules and Differences
Syntax |
Description |
Example |
Difference |
import module |
Imports the full module. Use moduleName.functionName to access functions. |
import math
print(math.sqrt(16)) |
Keeps the namespace clean, avoids naming conflicts. |
from module import function |
Imports specific functions directly, no need for the module name prefix. |
from ikea import add
print(add(1, 2)) |
Convenient for using specific functions frequently. |
from module import * |
Imports all functions, classes, and variables from the module directly into the current namespace. |
from math import *
print(sqrt(16)) |
Can cause naming conflicts, harder to track where functions/classes come from. Generally not recommended for larger codebases. |
Installing Libraries
Command |
Description |
Example |
pip install libraryname |
Installs a library from the command prompt or terminal. |
pip install pandas |
os.system('pip install libraryname') |
Installs a library from within your Python code using the os module to run shell commands. |
import os
os.system('pip install pandas') |
Managing Libraries When Sharing Python Code
Step |
Description |
Example |
pip freeze > requirements.txt |
Create a requirements.txt file that lists all the libraries your script needs. |
pip freeze > requirements.txt |
pip install -r requirements.txt |
Install dependencies on another system. |
pip install -r requirements.txt |
Example Workflow
Step |
Description |
Code |
Create the Python script |
Write your Python script and save it to a file. |
echo "import pandas as pd ... " > data_analysis.py |
Install pandas if not installed |
Ensure that the pandas library is installed. |
pip install pandas |
Generate requirements.txt |
Create a requirements.txt file listing all required libraries. |
pip freeze > requirements.txt |
Clone project or copy files |
Copy your project files to the target system. |
scp user@development_system:/path/to/project/* /path/to/local/directory/ |
Create virtual environment |
(Optional) Create a virtual environment for your project. |
python -m venv myenv
source myenv/bin/activate |
Install required libraries |
Install the required libraries on the target system using requirements.txt . |
pip install -r requirements.txt |
Run the script |
Execute your Python script on the target system. |
python data_analysis.py |