after finding source code file that matches an import statement by traversing the module search pathpython next compiles it to byte codeif necessary we discussed byte code briefly in but it' bit richer than explained there during an import operation python checks both file modification times and the byte code' python version number to decide how to proceed the former uses file "timestamps,and the latter uses either "magicnumber embedded in the byte code or filenamedepending on the python release being used this step chooses an action as followscompile if the byte code file is older than the source file ( if you've changed the sourceor was created by different python versionpython automatically regenerates the byte code when the program is run as discussed aheadthis model is modified somewhat in python and later-byte code files are segregated in __pycache__ subdirectory and named with their python version to avoid contention and recompiles when multiple pythons are installed this obviates the need to check version numbers in the byte codebut the timestamp check is still used to detect changes in the source don' compile ifon the other handpython finds pyc byte code file that is not older than the corresponding py source file and was created by the same python versionit skips the source-to-byte-code compile step in additionif python finds only byte code file on the search path and no sourceit simply loads the byte code directlythis means you can ship program as just byte code files and avoid sending source in other wordsthe compile step is bypassed if possible to speed program startup notice that compilation happens when file is being imported because of thisyou will not usually see pyc byte code file for the top-level file of your programunless it is also imported elsewhere--only imported files leave behind pyc files on your machine the byte code of top-level files is used internally and discardedbyte code of imported files is saved in files to speed future imports top-level files are often designed to be executed directly and not imported at all laterwe'll see that it is possible to design file that serves both as the top-level code of program and as module of tools to be imported such file may be both executed and importedand thus does generate pyc to learn how this workswatch for the discussion of the special __name__ attribute and __main__ in run it the final step of an import operation executes the byte code of the module all statements in the file are run in turnfrom top to bottomand any assignments made to names during this step generate attributes of the resulting module object this is how how imports work