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

Steven D'Aprano steve at pearwood.info
Tue Feb 21 00:40:10 CET 2012


anatoly techtonik wrote:
> On Mon, Feb 20, 2012 at 1:06 PM, Simon Sapin <simon.sapin at kozea.fr> wrote:
> 
>> 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)
>>>
>>>
>> You shouldn’t have to do that if you’re running 'python something.py'
>>
> 
> But I did for some reason, and right now I can't even say if it was
> Windows, Linux, FreeBSD, PyPy, IPython, gdb or debugging from IDE.


If you can't say why you did it, how can we judge whether you did it for a 
good reason or a bad reason?

Having a user-accessible search path is not a hack, or if it is, it is a hack 
in the positive sense: a feature, not a design bug. The same concept is used 
by Unix tools, via the PATH environment variable. It is "the simplest thing 
that could possibly work" for solving the problem of configurable search paths.

Personally, I don't believe sys.pack needs to be brought back under control, 
because I don't believe it is out of control. Most code doesn't need to mess 
with the path; of that which does, most does not lead to problems.

The only time I have seen path problems is when I have accidentally shadowed 
standard library modules, and they are simple to solve. Perhaps others have 
experienced harder problems, and if so, they have my sympathy, but I don't 
believe this is a problem so great that it needs to break backward compatibility.

I would say, though, that nearly every time I have changed sys.path, I would 
have been satisfied with some way of importing directly from a known location.

import spam from 'this/is/a/relative/path'
from spam import ham from '/and/this/is/an/absolute/path'

sort of thing, although I can see that import...from and from...import are too 
similar for comfort.


-- 
Steven




More information about the Python-ideas mailing list