Python default search paths

Jed Smith jed at jedsmith.org
Tue Oct 19 13:05:22 EDT 2010


On Tue, Oct 19, 2010 at 12:35 PM, Steven D'Aprano
<steve at remove-this-cybersource.com.au> wrote:
> On Tue, 19 Oct 2010 06:25:30 -0700, swapnil wrote:
>> This is useful for embedding applications where it might be desired that
>> only user-defined paths are searched for modules.
>
> I doubt that very much. I expect that many things will break if Python
> can't find (e.g.) the sys module. But you might be lucky.

sys is a bad example, because it is built-in and always available[1],
even if the path is destroyed.

I have personally done what OP is trying to do. It is not only
possible (but interesting) to give Python access only to your own
modules. It's similar to working in C without libc, and is just as
feasible and doable. Running with -vv is very useful here, so you can
see what Python does behind the scenes--the default site scripts
actually import quite a bit before handing off to you, and even if
path is wiped, those will continue to work (i.e., os; go interactive,
import sys, sys.path = [], then try importing os and, say, cgi).

Python doesn't expect to run this way so there's a few things you have
to work around, but I expect there's more than a few apps that operate
just like this.

[1]: http://docs.python.org/library/sys.html

-- 
Jed Smith
jed at jedsmith.org



More information about the Python-list mailing list