def find_index(to_search, target): """Find a index of a value in a sequence""" for i, value in enumerate(to_search): if value == target: return i return -1 #import from my_module import find_index, test courses = ['history', 'math', 'physics'] index = find_index(courses, 'math') print(index) print(test)
import my_module courses = ['History', 'Math', 'Physics', 'ComSci'] # calling index function from my_module.py file index = my_module.find_index(courses, 'ComSci') print(index) # import purticuler variable of function from my_module import test as t # printing variable from other file print(t) # import hoal file at ones from my_module import * # see system path from sys import path print(path) # apppend any path in to the system path # sys.path.append('/Users/yash/Desktop/') # use random library import random random_course = random.choice(courses) print(random_course) # use math library import math
from my_module import find_index courses = ['History','Math', 'Physics', 'CompSci'] index = find_index(courses, 'Math') print(index)
#Python Tutorial for Beginners 9: Import Modules and Exploring The Standard Library import my_module as mm #pega a função e abrevia para mm from my_module import find_index #fazendo assim não é preciso por o nome da biblioteca antes courses = ['History', 'Math', 'Physics', 'CompSci'] index = mm.find_index(courses, 'Math') print(index) import math import datetime import calendar # today = datetime.date.today() # print(today) # print(calendar.isleap(2020)) import os print(os.getcwd())
import my_module courses = ['History', 'Math', 'Physics', 'Compsci'] index = my_module.find_index(courses, 'Math') print(index) # Now we can also make the modulename shorter import my_module as mm index = mm.find_index(courses, 'Math') # Works fine print(index) # Now there is a way to import the function itself 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
#check more info here https://docs.python.org/3/tutorial/modules.html import my_module course = ["History", "Math", "Physics", "CompSci"] # when import like this: # print funciton will be Run # when you use funciton inside this module, you have to do my_module.find_index index = my_module.find_index(course, "Math") # print(index) # print (my_module.test) #--------------- import my_module as mm #import like this to give a short name of the main module index = mm.find_index(course, "Math") #--------------- from my_module import find_index # if you want to also import test, just write ",test" from my_module import find_index as fi, test # you can also import function and give nick name #---------------
import sys, os from my_module import find_index courses = ['History', 'math', 'physics', 'comsci'] index = find_index(courses, 'math') #print(index) print(os.__file__)
# sys.path is where python knows where to find module locations import sys import random # from my_module import * # import my_module as mm from my_module import find_index, test # sys.path.append("/home/poopmonkey/python_code") courses = ["History", "Math", "Physics", "CompSci"] random_course = random.choice(courses) index = find_index(courses, "Math") # print(index) # print(test) print(sys.executable) # location of python interpreter print(sys.path) print(random_course)
import sys sys.path.append('/home/pi/bin/05.python_with_corey/my-modules') from screen_clear import clear clear() # various ways to import modules: #import my_module #import my_module as mm from my_module import find_index, test print(sys.executable) print(sys.version) print() print(sys.path) print() print() courses = ['History', 'Math', 'Physics', 'ComSci'] #index = my_module.find_index(courses, 'Math') #index = mm.find_index(courses, 'Math') index = find_index(courses, 'Lit') #index = fi(courses, 'Math') print(index) print(test) print()
# Here we will import my_module that we created in another file. import my_module courses = ['History', 'Math', 'Physics', 'CompSci'] # To use the find_index function in my_module index = my_module.find_index(courses, 'Math') print(index) # We can also write 'import my_module as mm'. then we can write my_module as mm in the code. # Ex: index = mm.find_index(courses, 'Math') # Importing function or any other thing(ex: a string) from a module: from my_module import find_index, test index2 = find_index(courses, 'History') print(index2) print(test) # Import Everything: Usually never used cuz it makes it harder to track problems. from my_module import * # Where does python look for the modules when we import them? we can see the paths by: import sys print(sys.path) # What if our module is somewhere like in the desktop # One way: # We can add the directory of our module to sys.path, run print(sys.path and you will see this Desktop location added in the end of it means it will also search the desktop for out module)
import sys sys.path.append('/home/trt/Desktop') from my_module import find_index, test #import fiunction and var from my_module # from my_module import * import everything or wild card import courses = ['History', 'Math', 'Physics', 'CompSci'] index = find_index(courses, 'Physics') #Access func which is imported print(index) print(test) print(sys.path)
print(new_index) print() ## Import function inside module from specific path # Change python path to get the module sys.path.append( "C:\\Users\\rzl\\Documents\\GitHub\\Python\\S8-ModulesAndPackages\\public") 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 *
from my_module import find_index, test import sys sys.path.append('/Users/myomaung/Desktop/my_module') courses = ['History', 'Math', 'Physics', 'ComSci'] index = find_index(courses, 'ComSci') print(index) print(test) print(sys.path)
import random import sys import my_module if __name__ == '__main__': some_list = [ 'A', 1, 15, [1], ] a = 15 print(f'Index of element {a}: ', my_module.find_index(some_list, a)) b = 100 print(f'Index of element {b}: ', my_module.find_index(some_list, b)) print(my_module.__file__) print(random.choice(some_list)) print(my_module.__file__)
# https://www.youtube.com/watch?v=CqvZ3vGoGs0&t=1065s # demo on some of the modules import my_module # import my_module as mm # from my_module import * # from my_module import find_index as fi, Test import random import datetime import calendar import math import os list = ["Math", "History", "Geo", "Bio"] choice = my_module.find_index(list, "Geo") print('my_module: ' + str(choice)) choice = random.choice(list) print('random: ' + choice) rads = math.radians(90) print(math.sin(rads)) today = datetime.date.today() print(today) print(calendar.isleap(2000)) print(os.getcwd()) print(os.__file__)
import os import random import sys #import antigravity from my_module import find_index, test cities = ['London', 'New York', 'Miami', 'Tokyo', 'San Jose'] index = find_index(cities, 'London') print(index) print(test) print(sys.path) #shows the paths python searches to import module random_course = random.choice(cities) print(random_course) print(os.getcwd()) # returns the path of the root directory print(os.__file__) # prints the location of the os module. In this case notice that os is inside envs_gtp
import my_module as mm import sys import datetime import calendar courses = ['History', 'Math', 'Physics', 'CompSci'] print(mm.find_index(courses, 'Art')) print(sys.path) today = datetime.date.today() print(today) print(calendar.isleap(2020))
from my_module import find_index, test import my_module as mm import sys import random print(sys.path) course = ['history', 'math', 'physics', 'compsci'] index = mm.find_index(course, 'math') print(index) print(test) ran_course = random.choice(course) print(ran_course) sys.path.append('C:\\Users\\lamborghni\\Documents\\GitHub\\allinone') for i in sys.path: print(i)
# Explore importing modules # Import statements (this works because it is in the same directory) # Can use this to shorten the imported module when calling its functions # import my_module as mm # Importing specific function/variable from a module from my_module import find_index, test import sys # from my_module import * is frowned upon because it imports everything courses = ['History', 'Math', 'Physics', 'CompSci'] index = find_index(courses,'Physics') print(index) print(test) print(sys.path)
import my_module # import my_module from current dir # import my module as mm # from my_module import username, password as pw, find_index network_devices = ['router', 'switch', 'firewall', 'steelhead', 'access_point'] creds_username = my_module.username creds_password = my_module.password print(creds_username, creds_password) index = my_module.find_index(network_devices, 'firewall') print(index) # directory list on where to look for module import sys print(sys.path) # First in the list will be the current directory # Append a directory to sys.path if you have a module in different directory # sys.path.append('path\to\directory') # Standard library modules import datetime, math, os print(os.__file__) # Modules are just another python file, this will show the location of the file for you to view
# pyright: strict from typing import List, Tuple, Set # Importing our own py library # Here the py file is in same folder # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #from my_module import * #from my_module import find_index, test #from my_module import find_index #import my_module import my_module as mm courses1: Tuple[str, ...] = ('History', 'Math', 'Physics') index = mm.find_index(courses1, 'Math') print(index) courses2: List[str] = ['History', 'Math', 'Physics'] index = mm.find_index(courses2, 'Physics') print(index) courses3: Set[str] = {'History', 'Math', 'Physics'} index = mm.find_index(courses3, 'History') print(index) message: str = 'History' index = mm.find_index(message, 'r') print(index) message: str = 'History4' index = mm.find_index(message, '4')
import my_module courses = [ 'mathematics', 'fine arts', 'architecture', 'chemistry', 'physics', 'psychology', 'literature', 'engineering', 'philosophy', 'history', 'design', 'law' ] index = my_module.find_index(courses, 'chemistry') print(index)
##### # MODULES USAGE EXAMPLE ##### courses = ['Art', 'Maths', 'History', 'CS'] # Import the module and use an alias import my_module as m # Use the module index = m.find_index(courses, 'History') print(index) # Import a single function and a single variable from my_module import find_index, string print(string) # Import all from my_module import * # --------------------> <-------------------- #
import my_module as mm courses = [ 'mathematics', 'fine arts', 'architecture', 'chemistry', 'physics', 'psychology', 'literature', 'engineering', 'philosophy', 'history', 'design', 'law' ] index = mm.find_index(courses, 'literature') print(index)
#import my_module from my_module import find_index courses = ['IoT', 'AI', 'Machine Learning', 'Data Science', 'Cyber Security'] index = find_index(courses, 'AI') print(index) #Test
import my_module if __name__ =='__main__': some_list = ['A', 1, 15, [1], ] a = 15 print( f'Index of element {a}: ', my_module.find_index(some_list, a) ) b = 100 print( f'Index of element {b}: ', my_module.find_index(some_list, b) )
from my_module import find_index, test import sys import random import math import datetime import calendar import os # import antigravity courses = ['Data Networks', 'Java', 'Connected Devices', 'Cloud Computing'] index = find_index(courses, 'Java') print('Index value of the target element is: ', index) print(test) print(sys.path) course_random = random.choice(courses) print(course_random) radian = math.radians(90) print(radian) print(math.sin(radian)) today = datetime.date.today() print('Today\'s date is: ', today) print('Is 2020 a Leap Year?: ', calendar.isleap(2020)) print('Is 2017 a Leap Year?: ', calendar.isleap(2017))
from my_module import find_index as find, test # standards library contains module written by developer # optimized and put to our disposal for future use # let's import some modules from the standard library import os # gives access to the underlines of the operting system import math import random import datetime import calendar #---------------------------------------------------------------------------------------------# # let's create a course list courses = ['History', 'Math', 'Physics', 'CompSci'] # call a function from my_module index = my_module.find_index(courses, 'Math') print('item index on the list is:', index) # call a function from my_module using mm index = mm.find_index(courses, 'CompSci') print('item index on the list is:', index) # let's find index using find from import index = find(courses, 'Physics') print('item index on the list is:', index) print('test imported from my_module is:', test) # let's print a random item from the list random_course = random.choice(courses) print('print random course from course list:', random_course)
import my_module courses = ['History', 'Math', 'Physics', 'CompSci'] index = my_module.find_index(courses, 'Math') print(index) import my_module as mm courses = ['History', 'Math', 'Physics', 'CompSci'] 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 ###
from my_module import find_index courses = [ 'mathematics', 'fine arts', 'architecture', 'chemistry', 'physics', 'psychology', 'literature', 'engineering', 'philosophy', 'history', 'design', 'law' ] index = find_index(courses, 'fine arts') print(index)
# References https://www.youtube.com/watch?v=CqvZ3vGoGs0 # Importing runs all the code in the imported file import my_module # In same directory as importing module courses = ['History', 'Math', 'Physics', 'CompSci'] index = my_module.find_index(courses, 'Physics') print(index) print(my_module.find_index(courses, 'Art')) import my_module as mm # Can use shorter name for brevity print(mm.find_index(courses, 'Physics')) # Can import just a function # Can import multiple functions from my_module import find_index as f_i, test print(f_i(courses, 'Physics')) print(test) # Could import everything, but then hard to track # If use import my_module, then use my_module.find_index from my_module import * import sys print(sys.path) # Where python looks for code to import # Adds a folder in which to search for a module
#import my_module import sys sys.path.append('\home\kkakarla\Development\tensorflow-examples\python-examples') from my_module import find_index, test courses = ['History', 'Math', 'Physics', 'CompSci'] #index = my_module.find_index(courses, 'Math') index = find_index(courses, 'Math') print(index) print(test) print(sys.path)