module import search path strangeness

Peter Otten __peter__ at web.de
Tue Aug 12 04:56:29 EDT 2008


tow wrote:

>>> > I have a python script (part of a django application, if it makes any
>> > difference) which is exhibiting the following behaviour:
>>
>> > import my_module # succeeds
>> > imp.find_module("my_module") # fails, raising ImportError
>>
>> > which is completely baffling me. According to sys.path, both should
>> > fail; the directory containing my_module is not in sys.path (though
>> > the my_module directory itself is).
 
> The current directory is actually a subdirectory of /home/tow/test/
> my_module,
> but ".." is not in sys.path (nor is anything containing "..") In any

> Basically, I had thought that import and imp.find_module used exactly
> the same
> search path, but the above example shows that at least in this
> circumstance they
> don't; import is picking up additional search paths from somewhere -
> what am I missing?

Grepping through the django source finds

./trunk/django/core/management/__init__.py:   
sys.path.append(os.path.join(project_directory, os.pardir))

sys.path could be changed as a side effect of an import, so I ask you to
verify (again) that import and imp.find_module() see the same sys.path.

assert all(".." not in p for p in sys.path)
import my_module

assert all(".." not in p for p in sys.path)
imp.find_module("my_module")


Peter



More information about the Python-list mailing list