[问题 - 加载模块]如何加载上层目录的模块?? - V2EX
V2EX = way to explore
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
V2EX    Python

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

  •  
  •   1989922yan 2013-10-23 18:19:49 +08:00 5176 次点击
    这是一个创建于 4456 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假如这样的目录结构:

    /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!!
    第 1 条附言    2013-10-23 21:12:13 +08:00
    试了:
    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 条回复    1970-01-01 08:00:00 +08:00
    mozillazg
        1
    mozillazg  
       2013-10-23 19:47:09 +08:00
    试试 from .. import test
    1989922yan
        2
    1989922yan  
    OP
       2013-10-23 21:20:49 +08:00
    @mozillazg 我追加了内容,那个好像是不行的,能再帮看看吗??
    SErHo
        3
    SErHo  
       2013-10-23 21:31:28 +08:00
    @1989922yan test 里面有没有 __init__.py 文件?
    /td>
    forgetbook
        4
    forgetbook  
       2013-10-23 22:35:33 +08:00
    import sys
    sys.path.append("..")
    import test

    不用谢。
    话说回来,居然在这楼里看到故人了
    dexbol
        5
    dexbol  
       2013-10-23 22:45:50 +08:00   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
       2013-10-23 23:19:32 +08:00
    @SErHo 有的。是一个空文件。
    1989922yan
        7
    1989922yan  
    OP
       2013-10-25 00:49:57 +08:00
    @dexbol 第二种方法,我没试成功。

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

    谢谢T_T

    要是你也做web.py,咱们也能交流
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5885 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8. 36ms UTC 02:52 PVG 10:52 LAX 18:52 JFK 21: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