python 怎么引入上上级目录的文件啊? - 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
displayabc
V2EX    Python

python 怎么引入上上级目录的文件啊?

  •  
  •   displayabc 2015-01-19 23:09:47 +08:00 51267 次点击
    这是一个创建于 3920 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ├── jd
    │ ├── __init__.py
    │ ├── api
    │ │ ├── __init__.py
    │ │ ├── aa.py
    │ │ └── rest
    │ │ ├── __init__.py
    │ │ └── bb.py
    │ └── base.py

    不在bb里添加sys.path,怎么从bb里引入base里边的东西?
    32 条回复    2018-07-05 14:50:02 +08:00
    DeanThompson
        1
    DeanThompson  
       2015-01-19 23:52:00 +08:00   2
    把 jd 这个目录的绝对路径添加到 PYTHONPATH,然后可以 import base 或 from base import xx
    binux
        2
    binux  
       2015-01-19 23:57:45 +08:00   1
    import jd.base

    如果你是从 jd 的目录启动的,然后 bb 又要引用 base,那就是设计错误
    9hills
        3
    9hills  
       2015-01-20 00:28:43 +08:00   1
    from ..base import xx,用相对引用就好了。。
    regex
        4
    regex  
       2015-01-20 00:50:08 +08:00   1
    sys.path.append(path)
    regex
        5
    regex  
       2015-01-20 00:50:53 +08:00   1
    。。 没看完要求
    RIcter
        6
    RIcter  
       2015-01-20 09:10:14 +08:00 via iPhone   1
    @binux 菊苣…目录结构设计有没有具体的最佳实践喵OAQ
    dingyaguang117
        7
    dingyaguang117  
       2015-01-20 09:17:33 +08:00 via iPhone   1
    应该从根目录启动
    haoawesome
        8
    haoawesome  
       2015-01-20 09:39:19 +08:00   1
    可以用path这个包,虽然这不是一种推荐的做法

    ``` python
    import path, sys
    import folder = path.path(__file__).abspath()
    sys.path.append(folder.parent.parent)
    ```
    haoawesome
        9
    haoawesome  
       2015-01-20 09:40:56 +08:00   1
    import path, sys
    folder = path.path(__file__).abspath()
    sys.path.append(folder.parent.parent)

    BTW,发出的帖子不能修改啊?
    Zuckonit
        10
    Zuckonit  
       2015-01-20 09:43:20 +08:00   1
    结构不合理
    1989922yan
        11
    1989922yan  
       2015-01-20 09:46:57 +08:00   1
    @9hills 这个正解。
    binux
        12
    binux  
       2015-01-20 09:49:24 +08:00   1
    displayabc
        13
    displayabc  
    OP
       2015-01-20 11:51:09 +08:00
    @9hills 上上级目录
    displayabc
        14
    displayabc  
    OP
       2015-01-20 11:52:46 +08:00
    @binux 是从另外一个目录引用的jd包并且在那里启动,是可以运行的,但是pycharm没有提示。。。
    rikeinei
        15
    rikeinei  
       2015-01-20 12:16:51 +08:00   1
    from ... import base
    displayabc
        16
    displayabc  
    OP
       2015-01-20 12:28:23 +08:00   1
    @rikeinei 你确定这个可以用么。。。。
    rikeinei
        17
    rikeinei  
       2015-01-20 15:26:57 +08:00   1
    @haython
    我写flask的时候,一直这么弄的
    rikeinei
        18
    rikeinei  
       2015-01-20 15:29:08 +08:00   1
    @haython
    要么from ... import base
    要么from ...base import XXX,反正python使用相对路径的时候基本就这么弄
    9hills
        19
    9hills  
       2015-01-20 16:14:44 +08:00   1
    @haython 只要你要导入的那级是个package(有__init__.py),就可以无限 .....
    from .. import base ,上级目录
    from ... import base,上上级目录
    ....以此类推
    1989922yan
        20
    1989922yan  
       2015-01-20 16:23:25 +08:00   1
    @9hills
    Orz,还是正解。
    虽然我看着蛋痛。
    9hills
        21
    9hills  
       2015-01-20 16:24:39 +08:00   1
    @1989922yan 我感觉倒是挺方便的。。。

    其实比较推荐的做法是binux那种,直接从顶层往下import,看起来要舒服一些
    1989922yan
        22
    1989922yan  
       2015-01-22 09:54:34 +08:00   1
    @9hills
    其实,整个问题回答中,
    1. 都没有使用 __init__.py,这个文件。
    2. 提问者没有指明入口函数。


    A:
    1. 如果使用__init__.py文件,其中添加自己包的模块:
    import mymodule0
    import mymodule1
    这样做,其他模块,就可以 import my包中的模块了,不再需要考虑 相对引用的问题。

    2. 提问者如果指明了入口函数,那么在入口函数中,
    首先应该import 跟入口函数同级的模块,
    例如:base模块,api模块

    个人理解,我的项目结构:

    store_management
    ├── auth
    │ ├── __init__.py
    │ ├── errors.py
    │ ├── models.py
    │ ├── views.py
    ├── base
    │ ├── __init__.py
    │ ├── codes.py
    │ ├── errors.py
    │ ├── models.py
    │ ├── views.py
    ├── settings.py
    ├── start.py
    ├── static
    ├── url.py

    start.py 为入口函数。auth中,都会调用base包。
    auth 和 base是同级,既然加了___init__.py,就把他们两个当做是 两个同级模块好了
    9hills
        23
    9hills  
       2015-01-22 12:12:28 +08:00   1
    @1989922yan 你这个项目结构本身就是不推荐的,整个store_management 应该做成一个package,然后把start移到外面去。。
    9hills
        24
    9hills  
       2015-01-22 12:13:55 +08:00   1
    @1989922yan 给你看一个比较规范的Python Web Project:
    https://github.com/pythoncn/june
    1989922yan
        25
    1989922yan  
       2015-01-22 16:48:50 +08:00   1
    @9hills
    ok,我看看
    1989922yan
        26
    1989922yan  
       2015-01-22 17:13:14 +08:00   1
    @9hills

    参考了一个flask 实践的框架结构,稍等我把我的框架结构调整一下

    |-flasky
    |-app/
    |-templates/
    |-static/
    |-main/
    |-__init__.py
    |-errors.py
    |-forms.py
    |-views.py
    |-__init__.py
    |-email.py
    |-models.py
    |-tests/
    |-__init__.py
    |-test*.py
    |-venv/
    |-requirements.txt
    |-config.py
    |-manage.py
    1989922yan
        27
    1989922yan  
       2015-01-22 17:16:15 +08:00   1
    9hills
        28
    9hills  
       2015-01-22 17:47:12 +08:00   1
    @1989922yan 嗯,就是这个。不过那个app不要叫app,改成你的项目的名字或者代号比较好。

    比如上面的june和这个推荐的结构是一样的
    Porunga
        29
    Porunga  
       2016-09-02 01:12:55 +08:00
    @rikeinei
    我试了一下,好像是
    要么 from ... import base
    要么 from ..base import XXX

    下面这个比上面这个少个点好像
    noinlj
        30
    noinlj  
       2016-12-12 12:43:50 +08:00
    导入上级目录

    ```
    import sys
    sys.path.append('../')
    ```
    lushiqin
        31
    lushiqin  
       2018-02-08 13:07:59 +08:00
    @noinlj 添加不了啊 添加后的结果 sys.path 打印出来的结果是['D:\\pyworkspace\\stock', 'D:\\pyworkspace', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\1tenglu\\Python27\\DLLs', 'C:\\1tenglu\\Python27\\lib', 'C:\\1tenglu\\Python27\\lib\\plat-win', 'C:\\1tenglu\\Python27\\lib\\lib-tk', 'C:\\1tenglu\\Python27', 'C:\\1tenglu\\Python27\\lib\\site-packages', '../']
    XuAaron
        32
    XuAaron  
       2018-07-05 14:50:02 +08:00
    1、sys.path 看一下项目的目录
    2、如果引用的模块在本项目根目录下,可以使用 from dirname import module、import module 或者相对路径 import ..module 来引用(具体看实际情况)
    3、如果引用的模块和本项目目录不一致(这种基本不会出现,但是试验的话肯定会出现这种情况),可以使用 sys.path.append(path)(此处的 path 是你要引用的模块的目录),然后就可以使用 import module 引用了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3675 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 37ms UTC 10:31 PVG 18:31 LAX 03:31 JFK 06:31
    Do have faith in what you're doing.
    ubao 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