On Jul 27, 2015, at 1:24 PM, neubyr <neu...@gmail.com> wrote:

> 
> I am trying to understand sys.path working and best practices for managing it 
> within a program or script. Is it fine to modify sys.path using 
> sys.path.insert(0, EXT_MODULES_DIR)? One stackoverflow answer - 
> http://stackoverflow.com/a/10097543 - suggests that it may break external 
> 3'rd party code as by convention first item of sys.path list, path[0], is the 
> directory containing the script that was used to invoke the Python 
> interpreter. So what are best practices to prepend sys.path in the program 
> itself? Any further elaboration would be helpful. 


Why are you trying to modify sys.path?  I'm not judging, there are many good 
reasons to do so, but there may be safer ways of getting the effect you want 
that don't rely on modifying sys.path.  One simple method is to modify 
PYTHONPATH (https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH) 
instead.

In order of preference:

1) Append to sys.path.  This will cause you the fewest headaches.

2) If you absolutely have to insert into the list, insert after the first 
element.  As you noted from SO, and noted in the docs 
(https://docs.python.org/3/library/sys.html#sys.path), the first element of 
sys.path is the path to the directory of the script itself.  If you modify 
this, you **will** break third-party code at some point.  

Thanks,
Cem Karan
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to