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
stackpop
V2EX  ›  Python

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

  •  
  •   stackpop ·
    sjtubinlong · 2014-06-13 00:03:45 +08:00 · 2719 次点击
    这是一个创建于 3625 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题。
    之前一直是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 条回复    2014-06-13 09:05:45 +08:00
    tonghuashuai
        1
    tonghuashuai  
       2014-06-13 09:05:45 +08:00
    我都是这么做,举例:

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

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

    from A import *
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2794 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:40 · PVG 23:40 · LAX 08:40 · JFK 11:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.