Python default search paths

S

swapnil

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

Steven D'Aprano

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

Jed Smith

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top