コード例 #1
0
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')
コード例 #2
0
"""
import <module_name> as <alt_name>
"""

import mod as my_module

print(my_module.a)
print(my_module.foo('aaa'))
コード例 #3
0
def __main__():
    mod.foo()
    mod.foo = my_foo
    mod.foo()
コード例 #4
0
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
コード例 #5
0
def bar():
    from mod import foo
    foo('courage')
コード例 #6
0
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 
コード例 #7
0
def bar():
	from mod import foo
	foo('corge')
コード例 #8
0
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')
コード例 #9
0
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)

コード例 #10
0
# 
# 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())
コード例 #11
0
ファイル: app.py プロジェクト: int3l/explains
def main() -> int:
    foo()
    return 0
コード例 #12
0
ファイル: testsuite.py プロジェクト: sobolevn/cosmic-ray
 def test_simple(self):
     self.assertTrue(mod.foo())
コード例 #13
0
ファイル: app.py プロジェクト: int3l/explains
def main() -> int:
    for _ in range(5):
        foo()
    return 0
コード例 #14
0
ファイル: it_mod_double_fun.py プロジェクト: thebigG/pycopy
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
コード例 #15
0
import mod
print(mod.s)
mod.foo("String")
コード例 #16
0
ファイル: testsuite.py プロジェクト: MrSenko/cosmic-ray
 def test_simple(self):
     self.assertTrue(
         mod.foo())