importing: what does "from" do?

Charles T. Smith cts.private.yahoo at gmail.com
Thu Jan 21 10:12:13 EST 2016


On Thu, 21 Jan 2016 07:52:17 -0700, Ian Kelly wrote:

>> 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?


Yes.


> 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.


Well, I thought that that's what causes these import problems.  I just
wish that python gave me more information, like the tree of imports that's
causing the problem.



>> 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.


If indeed it's not defined, then I wouldn't think there'd be a
problem importing the module that defines it.


>> 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.


as in this?

  (PDB) pp dict (sys.modules)

Okay, quite good.  I have this (among others):


 'hexdump': <module 'hexdump' from '/home/user/hexdump.pyc'>,
 'int.hexdump': None,


But I suspect the first, at any rate, was loaded because of this in
my ~/.pdbrc:

  from hexdump import hexdump

which wouldn't interfere when running without the debugger (that module
in my sys.path but nowhere near my project)


I have this in my project:

  int/__init__.py

But:

  $ ls int/hexdump*
  ls: cannot access int/hexdump*: No such file or directory

So I don't really understand "int.hexdump".
I have these function definitions:

  codec/objects.py:47:def hexdump (s, l = None):
  int/struct_phy.py:26:def hexdump (s, l = None):

both in modules.

It fails to load:

  from struct_phy import hexdump

or:

    from int.struct_phy import hexdump
ImportError: cannot import name hexdump






More information about the Python-list mailing list