python引用子目录很简单, 里面放个__init__.py就可以了. 如何在子目录里面引用其他目录(父目录,爷目录和同辈分目录)呢?
例如: python有项目目录结构:
projectdir/
------------------ __init__.py
| ----------------- core/
|----------------- __init__.py
|---------------- a.py
|----------------- b.py
|----------------- common.py
|------------------ subcore/
|-------------------- __init__.py
|-------------------- common.py
|-------------------- test.py
|------------------- plugins/
|------------------- __init__.py
|------------------- mail.py
|------------------- tel.py
|----------------- common.py
|------------------- utils/
|------------------- __init__.py
|------------------- util.py
|-------------------- log.py
1) 现在 a.py要import util和log, 如何实现呢? 很简单, 所有的common.py内容都一样, 如下:
#!/usr/bin/python2.7 #-*- coding: UTF-8 -*- ####################################################################### import os, sys, inspect def script_abspath(frame=inspect.currentframe()): p = os.path.split(inspect.getfile( frame ))[0] absdir = os.path.realpath(os.path.abspath(p)) return absdir def script_abspath_parent(frame=inspect.currentframe()): return os.path.dirname(script_abspath(frame)) def include_dir(subdir=None, frame=inspect.currentframe()): # NOTES: # DO NOT USE __file__ !!! # dir = os.path.dirname(os.path.abspath(__file__)) # __file__ fails if script is called in different ways on Windows # __file__ fails if someone does os.chdir() before # sys.argv[0] also fails because it doesn't not always contains # the path # # realpath() will make your script run, even if you symlink it p = os.path.split(inspect.getfile( frame ))[0] incdir = os.path.realpath(os.path.abspath(p)) if incdir not in sys.path: sys.path.insert(0, incdir) if subdir: # use this if you want to include modules from a subfolder incdir = os.path.realpath(os.path.abspath(os.path.join(p, subdir))) if incdir not in sys.path: sys.path.insert(0, incdir) ########################################################### # include dir and parent dirs absdir = script_abspath() while os.path.isdir(absdir): pkgini = os.path.join(absdir, "__init__.py") if not os.path.exists(pkgini): break if os.path.isdir(pkgini): break include_dir(absdir) absdir = os.path.dirname(absdir)
#!/usr/bin/python2.7 #-*- coding: UTF-8 -*- # import common import utils.util import utils.log ...
#!/usr/bin/python2.7 #-*- coding: UTF-8 -*- # import common import utils.util import utils.log import plugins.mail ...
就这么简单.
__init__.py是空文件.