[Python-Dev] ImportWarning flood

James Y Knight foom at fuhm.net
Wed Jun 28 21:29:11 CEST 2006


On Jun 25, 2006, at 9:47 PM, James Y Knight wrote:

>
> On Jun 24, 2006, at 1:29 PM, Ralf W. Grosse-Kunstleve wrote:
>
>> --- Jean-Paul Calderone <exarkun at divmod.com> wrote:
>>> I think it is safe to say that Twisted is more widely used than
>>> anything
>>> Google has yet released.  Twisted also has a reasonably plausible
>>> technical reason to dislike this change.  Google has a bunch of
>>> engineers
>>> who, apparently, cannot remember to create an empty __init__.py
>>> file in
>>> some directories sometimes.
>>
>> Simply adding a note to the ImportError message would solve this
>> problem "just
>> in time":
>>
>>>>> import mypackage.foo
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in ?
>> ImportError: No module named mypackage.foo
>>     Note that subdirectories are searched for imports only if they
>> contain an
>>     __init__.py file: http://www.python.org/doc/essays/packages.html
>>
>
> I also dislike the warning solution. Making the ImportError message
> more verbose seems like a much nicer solution.

I just found another reason to dislike the warnings: my homedir on  
one machine has a lot of random directories in it. One of them is  
named "readline". Every time I run python 2.5, it now helpfully notes:
   sys:1: ImportWarning: Not importing directory 'readline': missing  
__init__.py

It used to be the case that it was very unlikely that running python  
in your homedir would cause issues. Even though the current directory  
is on the default pythonpath, you needed to have either a file ending  
in .py or a directory with an __init__.py with the same name as a  
python module to cause problems. And that is generally unlikely to  
happen. Now, however, you get warnings just by having _any_ directory  
in your CWD with the same name as a python module. That's much more  
likely to happen; I can't be the only one who will have this issue.

I'd like to suggest the simple solution quoted above with a constant  
string added to the ImportError message would be good enough, and  
better than the current warning situation. Clearly it would be even  
better if someone did the complicated thing of keeping track of which  
directories would have been used had they had __init__.py files in  
them, and appending _that_ to the eventual ImportError message, but I  
don't think removing the warning should be held up on doing that.


James



More information about the Python-Dev mailing list