Relative import from script with same name as package

OKB (not okblacke) brenNOSPAMbarn at NObrenSPAMbarn.net
Sun Aug 14 20:48:46 CEST 2011


=?UTF-8?B?T2t0YXkgxZ5hZmFr?= wrote:

>>           It appears that Python is reading "import thetest" as
>>           importing 
>> thetest.py (the same file that is currently being run).  When it
>> tries to run that file a second time, the relative import fails.
>>
> 
> No, there is no such thing happening. Read the error message more 
> carefully: the error happens when your code reaches the line "from 
> .theother import foo", and it fails because you are trying to
> execute an "explicit" relative import statement (with leading dot
> notation) as introduced by PEP 328. What you see is perfectly
> expected behaviour as explained in detail in the PEP because the
> python interpreter can only make sense of that statement if that
> code is *imported* for use by code that resides *outside* the
> package. That error message is what you see when you try to *run* a
> package member module which uses explicit relative imports. Let me
> try to explain a bit further: 

    	Yes, such a thing was happening.  (I described the fix in an 
answer to my own post.)  You should read PEP 366 to understand what I 
was talking about, and what the __package__ variable does.

-- 
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown



More information about the Python-list mailing list