python包之间引用_python 子包引用父包和其他子包

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

...

test.py 也一样:

#!/usr/bin/python2.7

#-*- coding: UTF-8 -*-

#

import common

import utils.util

import utils.log

import plugins.mail

...

就这么简单.

__init__.py是空文件.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值