[问题 - 加载模块]如何加载上层目录的模块?? - V2EX
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
1989922yan

[问题 - 加载模块]如何加载上层目录的模块??

  •  
  •   1989922yan Oct 23, 2013 5368 views
    This topic created in 4583 days ago, the information mentioned may be changed or developed.
    假如这样的目录结构:

    /test_root
    ....test.py
    ..../test_sub
    ........test_sub.py
    ........__init__.py

    在test_sub.py中,如何加载上层目录中的 test.py??

    1. 直接,import test,这样会报错
    2. 我的__init__.py文件为空,是否可以通过修改__init__.py文件,动态加载呢???

    THX every answer!!
    Supplement 1    Oct 23, 2013
    试了:
    from .. import test1

    错误:

    Traceback (most recent call last):
    File "test/test2.py", line 1, in <module>
    from .. import test1
    ValueError: Attempted relative import in non-package

    结构:
    ├── test
    │ ├── __init__.py
    │ └── test2.py
    └── test1.py

    1 directory, 3 files

    不知道这个的错误原因,麻烦帮助
    7 replies    1970-01-01 08:00:00 +08:00
    mozillazg
        1
    mozillazg  
       Oct 23, 2013
    试试 from .. import test
    1989922yan
        2
    1989922yan  
    OP
       Oct 23, 2013
    @mozillazg 我追加了内容,那个好像是不行的,能再帮看看吗??
    SErHo
        3
    SErHo  
       Oct 23, 2013
    @1989922yan test 里面有没有 __init__.py 文件?
    forgetbook
        4
    forgetbook  
       Oct 23, 2013
    import sys
    sys.path.append("..")
    import test

    不用谢。
    话说回来,居然在这楼里看到故人了
    dexbol
        5
    dexbol  
       Oct 23, 2013   1
    同为新手,特别能体会到你的迷惑。

    首先 `test_root` 下添加 __init__.py 是必须的,这样才能形成package的一个层级。

    然后你基本有两张方法搞定它:

    * 一种是相对路径。在test_sub.py 中这样引用test.py: `form .. import test` 然后将命令行当前目录更改到 test_root 的父目录,键入:

    python -m test_root.test_sub.test.sub


    * 或者绝对路径。 在test_sub.py 中键入

    import sys
    import os

    sys.path.insert(0, os.path.dirname(__file__) + '.\..\..')

    import test_root.test


    详细可参考这些:
    http://docs.python.org/2/tutorial/modules.html#intra-package-references
    http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python
    1989922yan
        6
    1989922yan  
    OP
       Oct 23, 2013
    @SErHo 有的。是一个空文件。
    1989922yan
        7
    1989922yan  
    OP
       Oct 25, 2013
    @dexbol 第二种方法,我没试成功。

    我觉得还有__import__这个函数,可以用,这个也很好用

    谢谢T_T

    要是你也做web.py,咱们也能交流
    About     Help     Advertise     Blog     API     FAQ     Solana     1082 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 48ms UTC 22:52 PVG 06:52 LAX 15:52 JFK 18:52
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86