from foo import bar and the ast module
Chris Angelico
rosuav at gmail.com
Sun Aug 22 10:13:12 EDT 2021
On Mon, Aug 23, 2021 at 12:08 AM Dan Stromberg <drsalists at gmail.com> wrote:
>
> In 'from foo import bar':
>
> With the ast module, I see how to get bar, but I do not yet see how to get
> the foo.
>
> There are clearly ast.Import and ast.ImportFrom, but I do not see the foo
> part in ast.ImportFrom.
>
> ?
>>> import ast
>>> ast.dump(ast.parse("from foo import bar"))
"Module(body=[ImportFrom(module='foo', names=[alias(name='bar')],
level=0)], type_ignores=[])"
>>> ast.parse("from foo import bar").body[0].module
'foo'
ChrisA
More information about the Python-list
mailing list