modulespackagesand distribution it is important to emphasize that import executes all of the statements in the loaded source file if module carries out computation or produces output in addition to defining variablesfunctionsand classesyou will see the result alsoa common confusion with modules concerns the access to classes keep in mind that if file spam py defines class spamyou must use the name spam spam to refer to the class to import multiple modulesyou can supply import with comma-separated list of module nameslike thisimport socketosre the name used to refer to module can be changed using the as qualifier here' an exampleimport spam as sp import socket as net sp foo(sp bar(net gethostname(when module is loaded using different name like thisthe new name only applies to the source file or context where the import statement appeared other program modules can still load the module using its original name changing the name of the imported module can be useful tool for writing extensible code for examplesuppose you have two modulesxmlreader py and csvreader pythat both define function read_data(filenamefor reading some data from filebut in different input formats you can write code that selectively picks the reader module like thisif format ='xml'import xmlreader as reader elif format ='csv'import csvreader as reader data reader read_data(filenamemodules are first class objects in python this means that they can be assigned to variablesplaced in data structures such as listand passed around in program as data for instancethe reader variable in the previous example simply refers to the corresponding module object underneath the coversa module object is layer over dictionary that is used to hold the contents of the module namespace this dictionary is available as the _dict_ of moduleand whenever you look up or change value in moduleyou're working with this dictionary the import statement can appear at any point in program howeverthe code in each module is loaded and executed only onceregardless of how often you use the import statement subsequent import statements simply bind the module name to the module object already created by the previous import you can find dictionary containing all currently loaded modules in the variable sys modules this dictionary maps module names to module objects the contents of this dictionary are used to determine whether import loads fresh copy of module lib fl ff