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

import sys.path #ImportError

  •  
  •   ThunderEX · Feb 2, 2013 · 4164 views
    This topic created in 4982 days ago, the information mentioned may be changed or developed.
    >>> import sys.path
    Traceback (most recent call last):
    File "<console>", line 1, in ?
    ImportError: No module named path
    >>> from sys import path
    >>> path
    ['C:\\Program Files (x86)\\JetBrains\\PyCharm 2.6.1\\helpers\\pydev', ...]

    我知道from import与import的不同在于前者会把module的命名空间融入当前的命名空间。
    我也知道sys这个module并没有对应的sys.py,而是以binary的形式存在。

    那为什么from sys import path不会异常而import sys.path会呢?
    5 replies  •  1970-01-01 08:00:00 +08:00
    whtsky
        1
    whtsky  
       Feb 2, 2013 via iPhone
    import sys
    sys.path
    ThunderEX
        2
    ThunderEX  
    OP
       Feb 2, 2013
    @whtsky 赋值当然会写……想问为什么直接import sys.path不行……
    whtsky
        3
    whtsky  
       Feb 2, 2013 via iPhone   ❤️ 1
    @ThunderEX import 只能用来导入模块.......sys.path是个变量.......
    chuangbo
        4
    chuangbo  
       Feb 2, 2013   ❤️ 2
    因为 sys.path 是一个变量,不是一个模块。
    import xxx,这个 xxx 必须是一个模块
    from sys import xxx,这里 xxx 可以是模块也可以是变量

    http://docs.python.org/2/reference/simple_stmts.html#the-import-statement
    ThunderEX
        5
    ThunderEX  
    OP
       Feb 2, 2013
    @whtsky 跪……
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   834 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:43 · PVG 04:43 · LAX 13:43 · JFK 16:43
    ♥ Do have faith in what you're doing.