importing: what does "from" do?

Ian Kelly ian.g.kelly at gmail.com
Thu Jan 21 09:52:17 EST 2016


On Jan 21, 2016 7:31 AM, "Charles T. Smith" <cts.private.yahoo at gmail.com>
wrote:
>
> What does "from (module) import (func)" do?

Approximately equivalent to:

import module
func = module.func

Except that it doesn't bind "module" to anything.

> Please don't tell me that I shouldn't ask because real programmers
> know not to have circular dependencies ...
>
> I have no idea what was imported before.  I just want to import hexdump().
> Unfortunately, this does not work:
>
>   from utilities import hexdump
>
>     ImportError: cannot import name hexdump

What is utilities? Is it a module on your sys.path?

> Now, I shouldn't have a problem because the programmers before me didn't
> understand either, and so just duplicated it everywhere, but I'd really
> like to do it right.
>
> I would have thought that "from" would allow me to import just one
> symbol.  Now, for all the people just chomping at the bit to tell
> me about circular dependencies

What makes you think this is caused by circular dependencies? It could be,
but you really haven't given us enough information about what utilities is
to diagnose that.

> would you explain to me why I get this:
>
>   (PDB)hexdump(msg)
> *** NameError: name 'hexdump' is not defined

Probably because the name 'hexdump' is not defined.

> P.S. can I get a list of all the loaded symbols and/or modules?

What do you mean by "loaded"? Bound to a name in the main module? In the
globals of the current scope? In any module? Try the "dir" or "vars"
built-in.

You can get a view of all the *cached* modules by looking in the
sys.modules dict.



More information about the Python-list mailing list