推荐学习书目
› 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
stackpop
V2EX  ›  Python

大家的项目是如何管理和组织文件的,文件之间的 import 又是如何处理的

  •  
  •   stackpop ·
    sjtubinlong · Jun 13, 2014 · 3376 views
    This topic created in 4500 days ago, the information mentioned may be changed or developed.
    如题。
    之前一直是c++后台程序员,把python当shell用,

    最近开始真正用Python做一些稍大的项目,我碰到的其中一个问题就是,比如
    A/__init__.py
    A/ x.py
    A/y.py

    B/__init__.py
    B/ z.py
    app.py

    其中A, B, app.py属于同一级目录,在A中的 x, y之间互相import, 在B中import A中的x, 在app.py中import A和B又是怎么处理?

    看了很多文档,都说相对的import是很坏的实践, 好的方法是以package的形式组织,包含setup.py, requirements.txt等等。

    看看大家在实践中是怎么处理的。
    1 replies  •  2014-06-13 09:05:45 +08:00
    tonghuashuai
        1
    tonghuashuai  
       Jun 13, 2014
    我都是这么做,举例:

    在 A 目录下的 __init__.py 中:
    __all__ = [
    'x',
    'y',
    ]

    然后在引用 x 或 y 的地方:

    from A import *
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   938 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 23:13 · PVG 07:13 · LAX 16:13 · JFK 19:13
    ♥ Do have faith in what you're doing.