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

python 下,目录 import 导入问题?有目录结构和简单代码

  •  
  •   redhatping · May 7, 2015 · 4028 views
    This topic created in 4169 days ago, the information mentioned may be changed or developed.
    第一个: 目录情况

    project (主目录)
    - test(目录)- test.py __init__.py

    - NAME(目录)- __init__.py



    test.py 代码如下:
    '''python
    from nose.tools import *
    import Name

    def test ( ):
    print "test"
    '''
    在project目录下,运行nosetests
    为什么test.py脚本导入Name, 什么原理, 不在一个目录下啊?
    如果是nose工具查找, 那么下面一个案例:

    第二个: 目录情况
    Project(主目录)
    - printlist.py
    - __init__.py

    - test (目录)---test.py

    - printlist(目录)-- __init__.py

    test.py 如下

    ''' python
    from nose.tools import *
    from project.printlist import *

    def test():
    print "test"
    '''

    这个时候 import 下的projct.printlist 是调用printlist.py, 不明白import 路径下 到底是怎么调用的,求解答!!
    3 replies  •  2015-05-07 22:24:03 +08:00
    deepreader
        1
    deepreader  
       May 7, 2015
    这个有用吗?
    from pkgutil import extend_path
    __path__ = extend_path(__path__, __name__)

    http://stackoverflow.com/questions/1675734/how-do-i-create-a-namespace-package-in-python
    redhatping
        2
    redhatping  
    OP
       May 7, 2015
    @deepreader 有用啊,__init__.py 不需要extend啊, 但是import 路径看不懂。 正在看书中的问题。
    rcmerci
        3
    rcmerci  
       May 7, 2015
    没仔细看lz的问题,我猜大概是 `pep328`的事情
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   801 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:37 · PVG 03:37 · LAX 12:37 · JFK 15:37
    ♥ Do have faith in what you're doing.