files generate namespaces 've mentioned that files morph into namespacesbut how does this actually happenthe short answer is that every name that is assigned value at the top level of module file ( not nested in function or class bodybecomes an attribute of that module for instancegiven an assignment statement such as at the top level of module file pythe name becomes an attribute of mwhich we can refer to from outside the module as the name also becomes global variable to other code inside pybut we need to consider the notion of module loading and scopes bit more formally to understand whymodule statements run on the first import the first time module is imported anywhere in systempython creates an empty module object and executes the statements in the module file one after anotherfrom the top of the file to the bottom top-level assignments create module attributes during an importstatements at the top level of the file not nested in def or class that assign names ( =defcreate attributes of the module objectassigned names are stored in the module' namespace module namespaces can be accessed via the attribute__dict__ or dir(mmodule namespaces created by imports are dictionariesthey may be accessed through the built-in __dict__ attribute associated with module objects and may be inspected with the dir function the dir function is roughly equivalent to the sorted keys list of an object' __dict__ attributebut it includes inherited names for classesmay not be completeand is prone to changing from release to release modules are single scope (local is globalas we saw in names at the top level of module follow the same reference/assignment rules as names in functionbut the local and global scopes are the same--ormore formallythey follow the legb scope rule we met in but without the and lookup layers cruciallythoughthe module' global scope becomes an attribute dictionary of module object after the module has been loaded unlike function scopeswhere the local namespace exists only while the function runsa module file' scope becomes module object' attribute namespace and lives on after the importproviding source of tools to importers here' demonstration of these ideas suppose we create the following module file in text editor and call it module pyprint('starting to load 'import sys name def func()pass module namespaces