Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94672 > unrolled thread

Re: sys path modification

Started byCem Karan <cfkaran2@gmail.com>
First post2015-07-27 14:56 -0400
Last post2015-07-27 14:56 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: sys path modification Cem Karan <cfkaran2@gmail.com> - 2015-07-27 14:56 -0400

#94672 — Re: sys path modification

FromCem Karan <cfkaran2@gmail.com>
Date2015-07-27 14:56 -0400
SubjectRe: sys path modification
Message-ID<mailman.1035.1438023374.3674.python-list@python.org>
On Jul 27, 2015, at 1:24 PM, neubyr <neubyr@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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web