python 子包引用父包和其他子包
2016-04-28 22:43
507 查看
python 子包引用父包和其他子包
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)
a.py 如下:
#!/usr/bin/python2.7 #-*- coding: UTF-8 -*- # import common import utils.util import utils.log ...
[/code]
test.py 也一样:
#!/usr/bin/python2.7 #-*- coding: UTF-8 -*- # import common import utils.util import utils.log import plugins.mail ...
就这么简单.
__init__.py是空文件.
相关文章推荐
- python rforward.py
- 使用ArcPy对栅格数据进行批量投影坐标转换
- python基础之字符串格式化
- 关于Python的super用法研究
- python学习之argparse模块
- python import模块出错
- Python 字典 dictionary changed size during iteration
- python写文件illegal multibyte sequence问题
- python nosetests
- python get操作
- python 数据库
- python学习4
- python基础之函数
- python小练习:Opencv中将图片转为字符画
- Python3 nonlocal
- Python 闭包
- python面向对象程序设计
- win7+python+matplotlib
- The Steps of Machine Learning with Python
- Python-Scrapy 个人兴趣教程(三):扫尾