python ast module fails in pydev, succeeds in cmdline python
Question: This is weird. I run this program in PyDev

    
    
    import ast
    import sys
    
    if __name__ == '__main__':
        print sys.version
        src = '''
    print 3*4+5**2
    '''
        print dir(ast)
        n = ast.parse(src)
        print n
    

and it outputs:

    
    
    2.7.5 |Anaconda 1.6.0 (64-bit)| (default, May 31 2013, 10:45:37) [MSC v.1500 64 bit (AMD64)]
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
    Traceback (most recent call last):
      File ["C:\research\ast\ast\test1.py", line 16, in <module>
        n = ast.parse(src)
    AttributeError: 'module' object has no attribute 'parse'
    

but when I run it in cmdline it prints this:

    
    
    C:\research\ast\ast>python test1.py
    2.7.5 |Anaconda 1.6.0 (64-bit)| (default, May 31 2013, 10:45:37) [MSC v.1500 64
    bit (AMD64)]
    ['AST', 'Add', 'And', 'Assert', 'Assign', 'Attribute', 'AugAssign', 'AugLoad', '
    AugStore', 'BinOp', 'BitAnd', 'BitOr', 'BitXor', 'BoolOp', 'Break', 'Call', 'Cla
    ssDef', 'Compare', 'Continue', 'Del', 'Delete', 'Dict', 'DictComp', 'Div', 'Elli
    psis', 'Eq', 'ExceptHandler', 'Exec', 'Expr', 'Expression', 'ExtSlice', 'FloorDi
    v', 'For', 'FunctionDef', 'GeneratorExp', 'Global', 'Gt', 'GtE', 'If', 'IfExp',
    'Import', 'ImportFrom', 'In', 'Index', 'Interactive', 'Invert', 'Is', 'IsNot', '
    LShift', 'Lambda', 'List', 'ListComp', 'Load', 'Lt', 'LtE', 'Mod', 'Module', 'Mu
    lt', 'Name', 'NodeTransformer', 'NodeVisitor', 'Not', 'NotEq', 'NotIn', 'Num', '
    Or', 'Param', 'Pass', 'Pow', 'Print', 'PyCF_ONLY_AST', 'RShift', 'Raise', 'Repr'
    , 'Return', 'Set', 'SetComp', 'Slice', 'Store', 'Str', 'Sub', 'Subscript', 'Suit
    e', 'TryExcept', 'TryFinally', 'Tuple', 'UAdd', 'USub', 'UnaryOp', 'While', 'Wit
    h', 'Yield', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '
    __version__', 'alias', 'arguments', 'boolop', 'cmpop', 'comprehension', 'copy_lo
    cation', 'dump', 'excepthandler', 'expr', 'expr_context', 'fix_missing_locations
    ', 'get_docstring', 'increment_lineno', 'iter_child_nodes', 'iter_fields', 'keyw
    ord', 'literal_eval', 'mod', 'operator', 'parse', 'slice', 'stmt', 'unaryop', 'w
    alk']
    <_ast.Module object at 0x0000000002A99EB8>
    

What could be going wrong?


Answer: It seems the `ast` you have imported in PyDev is not the ast module in the
standard library, but a package.

I guess:

> There is a `__init__.py` file in the same directory as your test1.py.  
>  You selected "Add project directory to the PYTHONPATH" during the project
> creation.

These two combined, results in that error. The ast module in the standard
library is shadowed by this `ast` package.

In cmdline python, this `ast` package is not in the search path, thus the ast
module is imported.

If you change test1.py to

    
    
    import ast
    
    if __name__ == '__main__':
        print ast.__file__
    

I guess the output in PyDev would be

    
    
    C:\research\ast\ast\__init__.pyc
    

