Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Python default search paths (http://www.velocityreviews.com/forums/t735910-python-default-search-paths.html)

swapnil 10-19-2010 01:25 PM

Python default search paths
 
Python allows adding user defined paths to the module search path by
setting PYTHONPATH environment variable. It also allows to alter the
location of standard python libraries using PYTHONHOME. But there is
no way to "only" have user defined paths to python's search paths
(sys.path)

This is useful for embedding applications where it might be desired
that only user-defined paths are searched for modules. But python
creates some default paths and adds it to sys.path. Most of the times
it does not matter since PYTHONPATH paths are appended at the
beginning but this may sometimes result in unwanted behavior.

I think it would be a good idea to be able to say that you don't need
any default search paths. In this case if Python gives error if
PYTHONPATH is not set- I think that would be reasonable. Since
otherwise sys.path would be empty!!

Please provide feedback for this feature request.

Steven D'Aprano 10-19-2010 04:35 PM

Re: Python default search paths
 
On Tue, 19 Oct 2010 06:25:30 -0700, swapnil wrote:

> Python allows adding user defined paths to the module search path by
> setting PYTHONPATH environment variable. It also allows to alter the
> location of standard python libraries using PYTHONHOME. But there is no
> way to "only" have user defined paths to python's search paths
> (sys.path)


import sys
sys.path = ['.', 'only', 'user/defined/paths']

Put that in the file given by PYTHONSTARTUP.


> 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.



--
Steven

Jed Smith 10-19-2010 05:05 PM

Re: Python default search paths
 
On Tue, Oct 19, 2010 at 12:35 PM, Steven D'Aprano
<steve@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@jedsmith.org


All times are GMT. The time now is 12:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.