如何编写嵌套的python包

时间:2013-12-26 19:58:19

标签: python import module nested package

我编写了一个使用嵌套包的程序,但我认为这不是一种“官方”方式。

我将公共对象和变量存储在__init__中,我希望从包'模块(one.py,two.py)和顶级脚本(main.py)中获取它们。如果我将我的包嵌套在另一个包中,那么我必须手动重写导入,例如从import packimport nested.pack

我尝试了from . import *AttributeError: 'module' object has no attribute 'one')和import ..packSyntaxError: invalid syntax),但都没有奏效。也不是这样:

import importlib
pack = importlib.import_module( __package__ )

输出结果为:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    import pack.one
  File "pack\one.py", line 4, in <module>
    pack = importlib.import_module( __package__ )
  File "python32\lib\importlib\__init__.py", line 117, in import_module
    if name.startswith('.'):
AttributeError: 'NoneType' object has no attribute 'startswith'

我认为我找到了一个很好的解决方案,import语句是'深度独立',但它可能不是一种常见的方式。

_______________________________ DIRECTORY TREE _______________________________

main.py
pack/
  __init__.py
  one.py
  subpack/
    __init__.py
    two.py

___________________________________ FILES ____________________________________

__ MAIN.PY ____________________________

  import pack
  import pack.one
  import pack.subpack
  import pack.subpack.two

  print( '__ MAIN.PY ____________________________' )

  print( 'PACK.VAR', pack.var )
  print( 'SUBPACK.VAR', pack.subpack.var )

  pack.one.fnc()
  pack.subpack.two.fnc()

__ PACK/__INIT__.PY ___________________

  import sys

  __all__ = [ 'one' ]

  var = sys.modules[ __name__ ]

__ PACK/ONE.PY ________________________

  import sys
  pack = __import__( '.'.join( __name__ .split( '.' )[0:-1] ) )

  def fnc():
    print( '__ ONE.PY _____________________________' )
    print( sys.modules[ __name__ ] )
    print( 'PACK.VAR', pack.var )

__ PACK/SUBPACK/__INIT__.PY ___________

  import sys

  __all__ = [ 'two' ]

  var = sys.modules[ __name__ ]

__ PACK/SUBPACK/TWO.PY ________________

  import sys
  subpack = __import__( '.'.join( __name__ .split( '.' )[0:-1] ) )

  def fnc():
    print( '__ TWO.PY _____________________________' )
    print( sys.modules[ __name__ ] )
    print( 'SUBPACK.VAR', subpack.var )

___________________________________ OUTPUT ___________________________________

__ MAIN.PY ____________________________
PACK.VAR &lt;module 'pack' from 'e:\laci\0_uj\korrekcio\tmp\pack\__init__.py'&gt;
__ ONE.PY _____________________________
&lt;module 'pack.one' from 'e:\laci\0_uj\korrekcio\tmp\pack\one.py'&gt;
SUBPACK.VAR &lt;module 'pack' from 'e:\laci\0_uj\korrekcio\tmp\pack\__init__.py'&gt;
SUBPACK.VAR &lt;module 'pack.subpack' from 'e:\laci\0_uj\korrekcio\tmp\pack\subpack\__init__.py'&gt;
__ TWO.PY _____________________________
&lt;module 'pack.subpack.two' from 'e:\laci\0_uj\korrekcio\tmp\pack\subpack\two.py'&gt;
SUBPACK.VAR &lt;module 'pack' from 'e:\laci\0_uj\korrekcio\tmp\pack\__init__.py'&gt;

使“深度独立”包__init__.py导入的好方法是什么?

我在Windows 7上用Python 3.2.5尝试了上面的代码。

1 个答案:

答案 0 :(得分:0)

您可以编写与执行文件相关的import语句。例如,如果main.py文件是您的程序起点,则可以在包one内编写:

import pack

没有任何问题。