# # access a function or variable by importing an external module (file) # # the path name of a module reflects its directory hierarchy import mymodule import this.mymodule from this import mymodule from that import mymodule from that.was import mymodule import this.was.my.lib.mymodule from this.was.my.lib import mymodule #x import this.is.my.lib.mymodule #x from that.is.my.lib import mymodule #x from that.is import mymodule # can not contains "is" in module path ???!!! # # and prefixing the function by module name # mymodule.greeting("Jonathan") a = mymodule.person1["age"] print('Person1 is %d years old' % a) # # Use a shorthand for a module name # #= import mymodule as mx #x import this.is.my.lib.mymodule as mx import this.mymodule as mx a = mx.person1["age"] print('Person1 is %d years old' % a) # # You can choose to import only parts from a module, by using the from keyword. # # Import only the person1 dictionary from the module: #= from mymodule import person1 #x from this.is.my.lib.mymodule import person1 from this.mymodule import person1 # don't need to add module name as prefix a = person1["age"] print('Person1 is %d years old' % a) # Note: When importing using the from keyword, # do not use the module name when referring to elements in the module. # Example: person1["age"], not mymodule.person1["age"] # # import build-in modules (located in some installation directories) # import platform x = platform.system() print('\nThe current system is %s' % x) # # List all the defined names belonging to the platform module: # #= import platform, mymodule #x import platform, this.is.my.lib.mymodule import platform, this.mymodule x = dir(platform) print("") print('List all the defined names belonging to the platform module:', x) #= y = dir(mymodule) #x y = dir(this.is.my.lib.mymodule) y = dir(this.mymodule) print("") print('List all the defined names belonging to the mymodule module:', y) print("") print('The __name__ variable for an imported module is simply its module name: %s, (NOT "__main__").' % mymodule.__name__)