def runModules(): # from 5.1.3.3 #import module # from 5.1.3.4 # main.py from module import suml, prodl zeroes = [0 for i in range(5)] ones = [1 for i in range(5)] count1 = [i+1 for i in range(5)] count2 = [i*2 for i in range(5)] print(suml(zeroes)) print(prodl(ones)) print(count1) print(suml(count1)) print(count2) print(prodl(count2))
from module import suml, prodl zeroes = [0 for i in range(5)] ones = [1 for i in range(5)] print(suml(zeroes)) print(prodl(ones))
from sys import path path.append('..\\modules') import module zeroes = [0 for i in range(5)] ones = [1 for i in range(5)] print(module.suml(zeroes)) print(module.prodl(ones))
from sys import path from module import suml, prodl zereos = [0 for i in range(5)] ones = [1 for i in range(5)] print(suml(zereos)) print(prodl(ones))
return the_sum def prodl(the_list): global __counter __counter += 1 prod = 1 for element in the_list: prod *= element return prod if __name__ == "__main__": print("I prefer to be a module, but I can do some tests for you.") my_list = [i+1 for i in range(5)] print(suml(my_list) == 15) print(prodl(my_list) == 120) from module import suml, prodl zeroes = [0 for i in range(5)] ones = [1 for i in range(5)] print(suml(zeroes)) print(prodl(ones)) import sys for p in sys.path: print(p) from sys import path path.append('..\\modules') import module
from sys import path path.append('..\\Modules') from module import suml,prodl sumlist = [i+1 for i in range(20)] prodlist = [j for j in range(1,5)] print(prodlist) print(suml(sumlist)) print(prodl(prodlist))
return prod if __name__ == "__main__": # "__name__" is a variable. If run a file directly, it's set to __main__ string; if a file is imported as a module, it's set to the file's name. print('I prefer to be a module, but I can do some tests for you.') l = [i + 1 for i in range(5)] # Create a list print(sum(l) == 15) # check if the output is equal to 15 print(prodl(l) == 120) """ 2. Use the module created in step 1. """ from module import suml, prodl # Selective import zeros = [0 for i in range(5)] ones = [1 for i in range(5)] print(suml(zeros)) # Use the function suml directly instead of module.suml print(prodl(ones)) """ 3. Create a package, which has a number of modules and sub-packages too. """ from sys import path # Selective import, and sys is another coomon built-in module. path.append( 'C:\\Users\\yunfeng.zhao\\PycharmProjects\\PCPP\\Packages\\Extrapack_4_3_24.zip' ) # Path is a list, so Path.append is used to add a specific package path list element. \ #Doulbe \ is used as we want to get a single backslash. The last element is a zip file but won't affect anything. print( path ) # Print out this new list, "sys.path" is NOT allowed as this is a selective import.\ # The list has Two category paths, one is about current working directory, and the other one is about Python software itself that including all standard libraries. import extra.good.best.sigma as sig # Sigma is a module, using alia makes it much easier
# main.py from sys import path path.append( 'C:\\Users\\dmcew\\proy_programacion\\Info_I\\Cisco_python\\module_5\\prueba_modules2\\modulex' ) #DOBLE \ #path.append('..\\modulex') Hemos utilizado el nombre relativo de la carpeta import module zeroes = [0 for i in range(5)] ones = [1 for i in range(5)] print(module.suml(zeroes)) #APLICANDO LAS FUNCIONES CREADAS EN EL OTRO MÓDULO print(module.prodl(ones))
import module as mm er = [0 for i in range(5)] mur = [1 for i in range(5)] print(mm.suml(er)) print(mm.prodl(mur))