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

Peng Yu pengyu.ut at
Mon Mar 18 14:52:05 CET 2013

On Mon, Mar 18, 2013 at 1:54 AM, Steven D'Aprano
<steve+comp.lang.python at> 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$ subdir/
==> <==
#!/usr/bin/env python

import sys

print sys.path

==> subdir/ <==
#!/usr/bin/env python

import sys

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


More information about the Python-list mailing list