def bar(): try: from mod import foo foo('corge') # Non-existent module from buz import baz except ImportError: print('Module not found') # Existing module, but non-existent object try: from mod import baz except ImportError: print('Object not found in module')
""" import <module_name> as <alt_name> """ import mod as my_module print(my_module.a) print(my_module.foo('aaa'))
def __main__(): mod.foo() mod.foo = my_foo mod.foo()
A module can be written in Python itself. A module can be written in C and loaded dynamically at run-time, like the re (regular expression) module. A built-in module is intrinsically contained in the interpreter, like the itertools module. ''' #Assuming mod.py is in an appropriate location, which you will learn more about shortly, # these objects can be accessed by importing the module as follows: import mod print(mod.s) mod.a mod.foo(['quux', 'corge', 'grault']) x = mod.Foo() x ###The Module Search Path #Continuing with the above example, let’s take a look at what happens when Python executes the statement: #import mod #When the interpreter executes the above import statement, it searches for mod.py in a list of directories # assembled from the following sources: #The directory from which the input script was run or the current directory if the interpreter is being # run interactively
def bar(): from mod import foo foo('courage')
import <module_name> this does not make the module contents From the caller, objects in the module are only accessible when prefixed with <module_name> via dot notation, as illustrated below import mod print(s) (Object inside mod.py) (ERROR) print(foo('quuz')) (function inside mod.py) (ERROR) mod.s (OK) mod.foo (OK) An alternate form of the import statement allows individual objects from the module to be imported directly into the caller’s from <module_name> import from mod import s, foo print(s)(OK) print(foo('quux'))(OK) from mod import * print(s)(OK) print(a)(OK) print(foo('quux'))(OK) from <module_name> import <name> as <alt_name> If your script already contain same object names which as in module then we can use alternative names while importing
def bar(): from mod import foo foo('corge')
def bar(): # import the mod with his function foo inside another # function but will not be executed until we cal this function from mod import foo foo('corge')
import mod import sys print(mod.a) print(mod.s) print(mod.foo(mod.a)) print(mod.__file__) # x = mod.Foo() # x # print(sys.path)
# # https://realpython.com/python-modules-packages/ # # sys.path.append(r'C:\Users\john') import mod print("mod: ", mod) print(mod.s) print(mod.a) mod.foo(['one', 'two', 'three']) x = mod.Foo() print(x) import sys print(sys.path) print("mod.__file__: ", mod.__file__) import re print("re.__file__: ", re.__file__) s = "local s" print("s: ", s) from mod import s print("s: ", s) from mod import s as alt_s print("alt_s: ", alt_s) import mod as alt_mod print("alt_mod.s, ", alt_mod.s) print("dir(): ", dir())
def main() -> int: foo() return 0
def test_simple(self): self.assertTrue(mod.foo())
def main() -> int: for _ in range(5): foo() return 0
import mod def foo(): return 1 try: mod.foo = foo except RuntimeError: print("RuntimeError1") print(mod.foo()) try: mod.foo = 1 except RuntimeError: print("RuntimeError2") print(mod.foo) try: mod.foo = 2 except RuntimeError: print("RuntimeError3") print(mod.foo) def __main__(): pass
import mod print(mod.s) mod.foo("String")
def test_simple(self): self.assertTrue( mod.foo())