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

Dave Angel davea at davea.name
Mon Mar 18 07:32:28 EDT 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

python peng.py
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.

-- 
DaveA



More information about the Python-list mailing list