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

Simon Sapin simon.sapin at kozea.fr
Mon Feb 20 11:06:34 CET 2012


Le 20/02/2012 10:47, anatoly techtonik a écrit :
>
> 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)
>

Hi,

You shouldn’t have to do that if you’re running 'python something.py'

> As initialized upon program startup, the first item of this list,
> path[0], is the directory containing the script that was used to
> invoke the Python interpreter. If the script directory is not
> available (e.g. if the interpreter is invoked interactively or if the
> script is read from standard input), path[0] is the empty string,
> which directs Python to search modules in the current directory
> first.

http://docs.python.org/py3k/library/sys.html#sys.path

The trick is to place the script in the directory that you want in the 
path, ie. next to top-level packages. But from your code above this 
seems to be the case already...

Regards,
-- 
Simon Sapin



More information about the Python-ideas mailing list