xxxxxxxxxx
Command: import
What is import: Import Statement
Description: The import statement is used to import modules or specific functions, classes, or variables from modules into your Python script.
Example: import math will import the math module, allowing you to use its functions, like math.sqrt(4) to calculate the square root of 4.
xxxxxxxxxx
# Ways to import in Python
import 'module_name'
import 'module_name' as 'name'
from 'module_name' import *
from 'module_name' import 'name', 'name'
from 'module_name' import 'name' as 'new_name', 'name' as 'new_name'
xxxxxxxxxx
from time import sleep
# or import time
a = 10
sleep(4) # sleep function from time library
print(a)
xxxxxxxxxx
import datetime #import module
from datetime import timedelta #import method from module
#You can also use alias names for both
import datetime as dt
from datetime import timedelta as td
xxxxxxxxxx
# import modules/files/functions
import MODULE_NAME/PATH_TO_FILE # just import the module
import MODULE_NAME/PATH_TO_FILE as # imports the module "under some name"
from MODULE_NAME/PATH_TO_FILE import FILE/FUNCTION # imports just one file/function of the module
from MODULE_NAME/PATH_TO_FILE import FILE/FUNCTION as
from MODULE_NAME/PATH_TO_FILE import * # imports all functions/files from one module/file