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 · 4438 次点击
    这是一个创建于 3809 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假如这样的目录结构:

    /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 文件?
    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   ·   我们的愿景   ·   实用小工具   ·   2712 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:44 · PVG 23:44 · LAX 08:44 · JFK 11:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.