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


Groups > comp.lang.python > #94672

Re: sys path modification

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cfkaran2@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'modify': 0.04; 'append': 0.07; 'interpreter.': 0.07; 'modifying': 0.07; 'cc:addr:python- list': 0.09; 'python': 0.10; 'itself.': 0.11; 'instead.': 0.15; 'element.': 0.16; 'prepend': 0.16; 'received:192.168.1.4': 0.16; 'sys.path': 0.16; 'wrote:': 0.16; 'element': 0.18; 'script.': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'noted': 0.22; 'trying': 0.22; 'cc:no real name:2**0': 0.22; 'insert': 0.23; 'third-party': 0.23; 'thanks,': 0.24; 'header:In-Reply-To:1': 0.24; 'script': 0.25; 'external': 0.27; 'point.': 0.27; 'fine': 0.28; 'helpful.': 0.29; 'invoke': 0.29; "i'm": 0.30; 'code': 0.30; 'convention': 0.30; 'getting': 0.33; 'url:python': 0.33; 'message- id:@gmail.com': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'path': 0.35; 'item': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'charset:us-ascii': 0.37; 'why': 0.39; 'received:192': 0.39; 'some': 0.40; 'url:3': 0.60; 'header:Message-Id:1': 0.61; 'further': 0.62; 'within': 0.64; 'managing': 0.66; 'jul': 0.72; 'url:a': 0.75; 'itself?': 0.84; 'absolutely': 0.88; 'safer': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=0e/M4aA4s9nKmGYpyhBrDMTYrK17sn93Mx1j20rwGuU=; b=SXGQ4fAFjdXArjqTxx7l69gt6fRIX3TXF8OvSC9EVqFlbga9A5h7DFpVWxvKCsgj1W td8vc/jD3XeqbbalCIXGbFNHfbZRVSdXxvRtlZW8LMO8N77K/PuOdpONWy9Xt0wTdz07 VPFVQA9iG/zqrkIZ5sd1sCBzh98Xz7WGoUG+Pu7rkMBN0akQjZKDDY+9U9xN/c5yERSS tnTR6EFV9D7Y2J7dm8yYKfE+TYA46qnkxP6wolY/tMc2yXbseaSVAB6I9wRQkU1BY7Fo rte8lzYjTW4eoDazySWreV6FKSu/5bykaI2cls3DmW8Zzq3opToPuzDtk/ZpSgCn+QdP LaSw==
X-Received by 10.13.250.198 with SMTP id k189mr32973703ywf.46.1438023371719; Mon, 27 Jul 2015 11:56:11 -0700 (PDT)
Content-Type text/plain; charset=us-ascii
Mime-Version 1.0 (Mac OS X Mail 6.6 \(1510\))
Subject Re: sys path modification
From Cem Karan <cfkaran2@gmail.com>
In-Reply-To <CALFxCvww4YJoRT436=urHt76ykUscK1XOjnqg8yGqJxBekYWFg@mail.gmail.com>
Date Mon, 27 Jul 2015 14:56:07 -0400
Cc python-list@python.org
Content-Transfer-Encoding quoted-printable
References <CALFxCvww4YJoRT436=urHt76ykUscK1XOjnqg8yGqJxBekYWFg@mail.gmail.com>
To neubyr <neubyr@gmail.com>
X-Mailer Apple Mail (2.1510)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1035.1438023374.3674.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1438023374 news.xs4all.nl 2847 [2001:888:2000:d::a6]:53583
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94672

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web