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


Groups > comp.lang.python > #41414

Re: How to add the current dir to sys.path when calling a python file?

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <pengyu.ut@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'diff': 0.05; 'sys': 0.05; 'so?': 0.07; 'subject:file': 0.07; 'subject:How': 0.09; 'python': 0.09; '"if': 0.09; 'given,': 0.09; 'inserted': 0.09; 'path,': 0.09; 'subject:python': 0.11; 'simple.': 0.16; 'stdin': 0.16; 'subject:when': 0.16; 'sys.path': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'script.': 0.17; 'variable': 0.20; 'file.': 0.20; 'import': 0.21; 'os,': 0.22; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; "d'aprano": 0.29; 'steven': 0.29; 'code': 0.31; 'print': 0.32; 'says': 0.33; 'to:addr:python- list': 0.33; 'front': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'path': 0.35; 'so,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'anything': 0.36; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'containing': 0.61; 'within': 0.64; '2013': 0.84; 'actually,': 0.84; 'subject:add': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=rOp2h1ukZv1FF+UudGVNP/oHrKQrfW0uplT+IONOq6M=; b=IFFLtzvdjQtZPVBqw6yYCWZ4p8sYiA8OfOSsum3UNv335hx6C58tq+l2sHwUhXpfw4 efFZSXvZHG0t68cfkwLbT5jEkwWkSAUhPSUi9GBZ/iFZ1FDUBDpv/WHSZYCF9MAkvbhS Frdm6Zgw6SYr+zDDgu5fOvM8YBR36wQcnbkP3Dn8t32CxAJunxJwQGW7A87dmDG4LYEI o7uTdlZd65oK4x415an0WaXwcziSofztKJiW313Eprzf8KuhMq3nK3MqPCAb5S72tX5m xwxn+Jf1mVpoQmOa2V9MAfbE0GO6Bt7N0teH9ALCgKI/jETyBopK1FIKQcVcS0ZVDIoa nYNg==
MIME-Version 1.0
X-Received by 10.224.63.76 with SMTP id a12mr18010283qai.16.1363614726201; Mon, 18 Mar 2013 06:52:06 -0700 (PDT)
In-Reply-To <5146ba0b$0$6599$c3e8da3$5496439d@news.astraweb.com>
References <mailman.3407.1363579363.2939.python-list@python.org> <5146ba0b$0$6599$c3e8da3$5496439d@news.astraweb.com>
Date Mon, 18 Mar 2013 08:52:05 -0500
Subject Re: How to add the current dir to sys.path when calling a python file?
From Peng Yu <pengyu.ut@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3443.1363614734.2939.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363614734 news.xs4all.nl 6898 [2001:888:2000:d::a6]:35403
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41414

Show key headers only | View raw


On Mon, Mar 18, 2013 at 1:54 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Sun, 17 Mar 2013 22:56:07 -0500, Peng Yu wrote:
>
>> Hi,
>>
>> man python says "If a script  argument  is  given,  the directory
>> containing the script is inserted in the path in front of $PYTHONPATH.
>> The search path can be manipulated from  within a Python program as the
>> variable sys.path." Instead I want to have the current directory
>> inserted to the front of $PYTHONPATH without changing anything the
>> script. Is there a way to do so?
>
> No. If you want to manipulate the path, you have to write code to do so,
> and put it in your script. That's very simple:
>
> import os, sys
> sys.path.insert(0, os.getcwd())

Actually, it is quite simple. Just use stdin to take the python file.

~/linux/test/python/man/library/sys/path$ cat.sh main.py subdir/main.py
==> main.py <==
#!/usr/bin/env python

import sys

print sys.path

==> subdir/main.py <==
#!/usr/bin/env python

import sys

print sys.path
~/linux/test/python/man/library/sys/path$ diff <(python - < main.py)
<(python - < subdir/main.py)



-- 
Regards,
Peng

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


Thread

How to add the current dir to sys.path when calling a python file? Peng Yu <pengyu.ut@gmail.com> - 2013-03-17 22:56 -0500
  Re: How to add the current dir to sys.path when calling a python file? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-18 06:54 +0000
    Re: How to add the current dir to sys.path when calling a python file? Peng Yu <pengyu.ut@gmail.com> - 2013-03-18 08:52 -0500
  Re: How to add the current dir to sys.path when calling a python file? rusi <rustompmody@gmail.com> - 2013-03-18 00:35 -0700

csiph-web