modules and imports

This commit is contained in:
2025-08-23 01:20:58 +05:30
parent 0e645479f5
commit 7b8cb6ce9e
12 changed files with 184 additions and 8 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
.venv
.venv
__pycache__/
*.pyc

28
src/basic/file1.py Normal file
View File

@ -0,0 +1,28 @@
# All functions, variables, class are available from export
# Python exports everything(functions, variables, classes) by default
# See file3 for custome Exporting
from file3 import *
def print_my_name():
print("Anikuttan")
file1_variable = 30
class Ani:
pass
# ==================================================================
print_my_full_name()
hidden_function() # hidden_function" is not defined error
print(my_name)
nai = AniKuttan()

35
src/basic/file2.py Normal file
View File

@ -0,0 +1,35 @@
# Import the whole module
# We must use the module name as a prefix
import file1
file1.print_my_name()
print(file1.file1_variable)
# ==================================================================
# Import specific functions/variables
# No prefix needed
from file1 import print_my_name
print_my_name()
# ==================================================================
# Import with alias
import file1 as my_file
my_file.print_my_name()
#==================================================================
# Import all
from file1 import *
print_my_name()
file1_variable
my = Ani()

16
src/basic/file3.py Normal file
View File

@ -0,0 +1,16 @@
__all__ = ["print_my_full_name", "my_name", "AniKuttan"]
def print_my_full_name():
print("Anikuttan Vijayakumar from file3.py")
my_name = "ANIKUTTAN from file3.py"
def hidden_function():
print("You shouldn't see me")
class AniKuttan:
pass

12
src/basic/file4.py Normal file
View File

@ -0,0 +1,12 @@
# Here we are importing the function from the service module
# services is the module (package) name order_service is the file name
# package is a folder with multiple .py files and an __init__.py file
from services.order_service import my_order_one
from services.product_service import helper_product_from_product_file
from services import my_product_one
# from services import helper_product_from_product_file
# my_order_one()
my_product_one()
helper_product_from_product_file()

View File

@ -115,3 +115,15 @@ def add(a, b) -> int:
number = add(a=10, b=20)
print(number)
def say_name():
...
def say_name():
pass
v = 10
dn = 40

View File

@ -64,15 +64,15 @@
# print(x, y)
# students = [{
# "name": "Ani",
# "subject": "Tamil"
students = [{
"name": "Ani",
"subject": "Tamil"
# }, {
# "name": "Kuttan",
# "subject": "English"
}, {
"name": "Kuttan",
"subject": "English"
# }]
}]
# for value in students:
# print(value['name'])

View File

@ -0,0 +1,23 @@
# Example 1
# if i run this file directly the name will call once becaus python will run the file top to bottom
# so the function call will trigger and it prints my name
def say_my_name_once():
print("Anikuttan example 1")
print(__name__)
say_my_name_once()
# -----------------------------------------------------------------------------------------------------------
# Example 2
def say_my_name_once_example2():
print("Anikuttan example 2")
# see Script_2.py
if __name__ == "__main__":
say_my_name_once()

View File

@ -0,0 +1,18 @@
# Example 1
# in here if i run this file script_2.py file it will print my name 2 times
# because the say_my_name_once triggers first time in this file and 2 time will call from the script_1.py file Example 1
from script_1 import say_my_name_once_example2
from script_1 import say_my_name_once
say_my_name_once()
# -----------------------------------------------------------
# Example 2
say_my_name_once_example2() # this will call one because the script_1.py file Example 2 there is __name__ condition
# __name__ will be "__main__" if the file runs directly
# if module runs by other file the __name__ will be "own modules name" like script1

View File

@ -0,0 +1,16 @@
# Its a special file that makes Python treat a directory as a package.
# it can be empty or we can exports with __all__
from .product_service import my_product_one
__all__ = ["my_product_one"] # see below
# in file4.py the imports look like this
# 1. from services import my_product_one
# 2. from services import helper_product_from_product_file # this will give error [cannot import name 'helper_product_from_product_file' from 'services']
# to use helper_product_from_product_file function or class we can able to access by package name (module name) like
# ==> from services.product_service import helper_product_from_product_file
# But we can able to access the my_product_one function like this
# ==> from services import my_product_one

View File

@ -0,0 +1,6 @@
def my_order_one():
print("This i from order_service.py")
def helper_service_from_order_file():
print("My service from order service")

View File

@ -0,0 +1,8 @@
# see services/__init__.py file and file4.py
def my_product_one():
print("This i from order_service.py --> my_product_one")
def helper_product_from_product_file():
print("My service from order service --> helper_product_from_product_file")