[Python-ideas] sys.path is a hack - bringing it back under control

anatoly techtonik techtonik at gmail.com
Mon Feb 20 10:47:17 CET 2012


Hi,

I often find this in my scripts/projects, that I run directly from checkout:

DEVPATH = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, DEVPATH)

This seems like a hack to me, because the process of sys.path modification
is completely out of control for Python application developer, which means
it is easy to break an application and get lost. I don't remember the exact
user story for that bad association with sys.path (perhaps Django issue
#1908), but something makes me feel that I am not alone:
http://stackoverflow.com/questions/5500736/troubleshooting-python-sys-path

What I'd like to propose is some control/info over what modified sys.path.
The simplest case:
1. make sys.path a list of pairs   (path, file-that-added-the-path)
2. make sys.path read-only
3. add sys.path.add() method for modification
4. logger for sys.path.add() events (or recipe how to implement it in
documentation)

This will help a lot.

Limiting sys.path may cause a loss of some functionality if you need to
remove some or replace it completely, but I don't know where the ability to
reset() sys.path can be useful.
-- 
anatoly t.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120220/008aaba5/attachment.html>


More information about the Python-ideas mailing list