cookbook_模块和包
mypackage/ __init__.py A/ __init__.py spam.py grok.py B/ __init__.py bar.py #spam.py from . import grok from ..B import bar
foo_package/ spam/ blah.py bar_package/ spam/ grok.py import sys sys.path.extend(["foo_package","bar_package"]) import spam.blah import spam.grok
import imp import math imp.reload(math)
myapplication/ spam.py bar.py grok.py __main__.py python myapplication 如果包含__main__.py文件,就可以当作脚本来运行,会运行__main__.py里的方法
mypackage/ __init__.py somedata.dat spam.py import pkgutil data = pkgutil.get_data(__package__,"somedata.dat") # data会得到一个字节串
#mtapplication /some/dir /other/dir
import sys sys.path.insert(0,"/some/dir") sys.path.insert(0,"/other/dir")
import importlib math = importlib.import_module("math")