I thought I understood how import worked...

Roy Smith roy at panix.com
Wed Aug 8 09:12:47 EDT 2012


In article <87hasehvfu.fsf at benfinney.id.au>,
 Ben Finney <ben+python at benfinney.id.au> wrote:

> Cameron Simpson <cs at zip.com.au> writes:
> 
> > All of you are saying "two names for the same module", and variations
> > thereof. And that is why the doco confuses.
> >
> > I would expect less confusion if the above example were described as
> > _two_ modules, with the same source code.
> 
> That's not true though, is it? It's the same module object with two
> different references, I thought.

Nope.  I modified my test case to print out the id of the module:

('broken', <module 'broken' from 
'/home/roy/play/import/foo/broken.pyc'>) 140608299115512
('foo.broken', <module 'foo.broken' from 
'/home/roy/play/import/foo/broken.pyc'>) 140608299116232

> Also, even if what you say were true, “source code” implies the module
> was loaded from source code, when Python allows loading modules with no
> source code available.

This is true.  In fact, when I first started chasing this down, one 
import was finding the .py file, and the other the .pyc file (created 
during the first import).  I originally assumed that the .py/.pyc 
distinction was the critical piece of the puzzle (and went down a 
rathole exploring that).

Then I went down a different rathole when I noticed that one code path 
was doing "import settings", while the other was doing 
"__import__(module_name)", thinking import and __import__ were somehow 
doing different things.



More information about the Python-list mailing list