importing: what does "from" do?

Ian Kelly ian.g.kelly at gmail.com
Thu Jan 21 10:59:39 EST 2016


On Thu, Jan 21, 2016 at 8:12 AM, Charles T. Smith
<cts.private.yahoo at gmail.com> wrote:
>>> 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.

What happens if you just do 'import utilities'. Can you then call
utilities.hexdump? Can you see anything in the utilities module?

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

The value of None in sys.modules caches a relative import miss. All it
means is that at some point something in the 'int' package tried to do
a relative import of the hexdump module and failed (presumably because
it doesn't exist).

Side observation: 'int' is a bad name for a package, because it will
shadow the name of the 'int' built-in.



More information about the Python-list mailing list