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

Dave Angel davea at
Mon Mar 18 12:32:28 CET 2013

On 03/17/2013 11:56 PM, 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?

if the script you're running is in the current directory, it'll just 
already do what you seem to be asking.

cd /home/davea/temppython
cat  peng.;py
import sys
print "sys.path is", sys.path

sys.path is ['/home/davea/temppython', '/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', '/usr/l .....

The text you quoted is trying to say that there are 3 different ways you 
can modify the search path.  The script name's location, the $PYTHONPATH 
environment variable, and the sys.path Python variable.


More information about the Python-list mailing list