6.1. More on Modules 43 Python Tutorial, Release 3.9.5 Note: For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use$_{importlib.reload()}$ , e.g.$_{import importlib; importlib.reload(modulename)}$ . When you run a Python module with python fibo.py <arguments> the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__" . That means that by adding this code at the end of your module: you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file: If the module is imported, the code is not run: This is often used either to provide a convenient user interface to a module, or for testing purposes (running the module as a script executes a test suite). When a module named$_{spam}$is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named$_{spam.py}$ in a list of directories given by the variable$_{sys.path}$ . $_{sys.path}$is initialized from these locations: Note: On file systems which support symlinks, the directory containing the input script is calculated after the symlink is followed. In other words the directory containing the symlink is not added to the module search path. After initialization, Python programs can modify$_{sys.path}$ . The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information. • The installation-dependent default. • PYTHONPATH (a list of directory names, with the same syntax as the shell variable$_{PATH}$ ). • The directory containing the input script (or the current directory when no file is specified). 6.1.2 The Module Search Path >>> import fibo >>> $ python fibo.py50 0 1 1 2 3 5 8 13 21 34 if __name__ =="__main__ ": import sys fib(int(sys.argv[1])) 6.1.1 Executing modules as scripts