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


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

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

Started byPeng Yu <pengyu.ut@gmail.com>
First post2013-03-17 22:56 -0500
Last post2013-03-18 00:35 -0700
Articles 4 — 3 participants

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


Contents

  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

#41391 — How to add the current dir to sys.path when calling a python file?

FromPeng Yu <pengyu.ut@gmail.com>
Date2013-03-17 22:56 -0500
SubjectHow to add the current dir to sys.path when calling a python file?
Message-ID<mailman.3407.1363579363.2939.python-list@python.org>
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?

-- 
Regards,
Peng

[toc] | [next] | [standalone]


#41398

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-03-18 06:54 +0000
Message-ID<5146ba0b$0$6599$c3e8da3$5496439d@news.astraweb.com>
In reply to#41391
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())



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#41414

FromPeng Yu <pengyu.ut@gmail.com>
Date2013-03-18 08:52 -0500
Message-ID<mailman.3443.1363614734.2939.python-list@python.org>
In reply to#41398
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

[toc] | [prev] | [next] | [standalone]


#41402

Fromrusi <rustompmody@gmail.com>
Date2013-03-18 00:35 -0700
Message-ID<0e47a0da-572c-4ee9-ba25-9ea9a36e3046@mc2g2000pbb.googlegroups.com>
In reply to#41391
On Mar 18, 8:56 am, Peng Yu <pengyu...@gmail.com> 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?
>
> --
> Regards,
> Peng

Have you seen http://docs.python.org/2/library/site.html ?

[toc] | [prev] | [standalone]


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


csiph-web