index = mm.find_index(courses, 'Math') print(index) from my_module import find_index # test variable has not been imported yet courses = ['History', 'Math', 'Physics', 'CompSci'] index = find_index(courses, 'Math') print(index) from my_module import find_index, test courses = ['History', 'Math', 'Physics', 'CompSci'] index = find_index(courses, 'Math') print(index) from my_module import find_index as fi, test courses = ['History', 'Math', 'Physics', 'CompSci'] index = fi(courses, 'Math') # Not really readable print(index) from my_module import * courses = ['History', 'Math', 'Physics', 'CompSci'] index = find_index(courses, 'Math') print(index) from my_module import find_index, test ### better to do in this way ### import sys courses = ['History', 'Math', 'Physics', 'CompSci'] index = find_index(courses, 'Math') print(sys.path) import sys sys.path.append('C:\\dev\\Python\\Tutorial')
from my_module import find_index # This only gives us access to that function index = find_index(courses, 'Math') # Works fine print(index) # This only gives us access to that function # Gives us access to both test and function from my_module import find_index, test_string index = find_index(courses, 'Math') # Works fine print(index) print(test_string) # This will make the function name shorter from my_module import find_index as fi # It should be readable though index = fi(courses, 'Math') # Works fine print(index) # Not recommended since now we dont know where did find_index() come from from my_module import * index = find_index(courses, 'Math') # Works fine print(index) print(test_string) import sys print(sys.path) # Will show the order in which python checks for files # First it'll check in the directory where we are running the script # Then it'll check in the python path
from my_module import find_index as fi, test import sys cursos = ['Hist','Mat','Fis','Art'] index = fi(cursos, 'Mat') print(index) print(test) print(sys.path)
from my_module import find_index as fi, test as t courses = [ 'mathematics', 'fine arts', 'architecture', 'chemistry', 'physics', 'psychology', 'literature', 'engineering', 'philosophy', 'history', 'design', 'law' ] index = fi(courses, 'psychology') print(index) print(t)
from my_module import find_index as fi import datetime import os student = {'name': 'john', 'age': 30, 'course': ['Math', 'Science']} print("************************************") print(fi(student, 'age')) print(fi.__doc__) print(datetime.date.today()) print(os.cpu_count())
from my_module import find_index as fi, test import sys import random import math import datetime import calendar import os courses = ['History', 'Math', 'Physics', 'CompSci'] print(fi(courses, 'Physics')) print(test) print(sys.path) print(random.choice(courses)) rads = math.radians(90) print(math.sin(rads)) print(datetime.date.today()) print(calendar.isleap(2017)) print(calendar.isleap(2020)) print(os.getcwd())
# # allows you to normally shorten the name of the module like NumPy as np; # # So that you can call any var, class or func in that module with mm.func() # # mm.var, or obj = mm.Class(v1, v2)t my_modul from my_module import find_index as fi, test as tst # simply put it is two different styles with which you can import functions and variables; # in this fashion with from module import function; # and you can call it without the need to do modulename(or moudleas).funct or moudle.var; # you can just call it as var, or funct (or whatever you saved it as) # you can import specific functions or variables, again as something else. # But everything else in the module will nto be available to you. print(tst) courses = ['history', 'math', 'physics', 'compsci'] index = fi(courses, 'math') print(index) # from my_module import * # this imports everything, but. # this is frowned upon as we don't know what came from that module and what didn't. # When you import modules, python looks for modules in its given PATH directories; # This includes your current working directory among many other paths. # You can see your path variables by doing the following; import sys print(sys.path) # If your module is NOT in the predefined path directories, you will not be able to import the module; # And python will raise an error if you try to do so. # So then you can add the path that the module is in to the environment PATH variable; # As sys.path is really a list
# import my_module as mm from my_module import find_index as fi, test #standard library import random import math import datetime import calendar import os courses = ['History', 'Math', 'Physics', 'CompSci'] index = fi(courses, 'Math') print(index) print(test) #Standard Library random_course = random.choice(courses) print(random_course) rads = math.radians(90) print(math.sin(rads)) today = datetime.date.today() print(today) print(calendar.isleap(2017)) print(os.getcwd()) print(os.__file__)
print(sys.path) print() # import function from my_module import find_index index_function = find_index(city, 'New York') print(index_function) print() ## Import variable inside module from my_module import find_index as fi, test as t new_fi = fi(city, 'Chicago') print(new_fi) print(t) print() # Import everything inside path or module from public import * index2 = my_module.find_index(city, 'Gotham') test2 = my_module.test print(index2) print(test2) print()
from my_module import find_index as fi, test courses = ['History', 'Math', 'Physics', 'CompSci'] print(fi(courses, 'Math')) print(test)
import datetime import calendar import os #sys.path.append('module location') from my_module import find_index as fi, test #from my_module import * ---->in order to import fn. as well as variables #harder to track variables and fn. when using * for import # in order to import a function from module # from my_module import find_index , test ....for string too # if u import only the function then no need for mm.find_index just the function itself courses = ['math', 'history', 'physics', 'CS'] index = fi(courses, 'physics') print(index) print(test) #print(sys.path).... tells the path random_course = random.choice(courses) print(random_course) rads = math.radians(90) print(math.sin(rads)) today = datetime.date.today() print(today) print(calendar.isleap(2020)) #using library module calendar
from my_module import find_index, test courses = ['History', 'Math', 'Physics', 'CompSci'] index = my_module.find_index(courses, 'CompSci') print(index) print(test) #>>>3 #Test String #can even name a method like: from my_module import find_index as fi, test as t_str courses = ['History', 'Math', 'Physics', 'CompSci'] index = fi(courses, 'CompSci') print(index) print(t_str) #>>>3 #Test String #To import everything, BUT IT IS FROWNED UPON #now we can't tell what came from theat module and what did not #importing select stuff is better for debugging from my_module import * # when we import itinterpreter checks multiple locations in a list sys.path import sys #the following prints a list type print(sys.path)