[Tutor] how to add directory to python search list

Steven D'Aprano steve at pearwood.info
Sun Jul 17 13:11:33 CEST 2011


Surya P.K. Kasturi wrote:

> can you tell me in detail how to do this.
> I am new to linux.

"this" being:

> On Sat, Jul 16, 2011 at 11:48 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:
>> You need to add the folder to your PYTHONPATH environment variable.
>> You usually do this your .login or .profile file.
>>
>> Python will add the contents of PYTHONPATH to sys.path on startup.

(Surya, on this list we prefer if people don't top-post.)

What Alan means is for you to edit your login file so that it creates an 
environment variable. How you do that depends on which shell you are 
using. If you don't know which shell, try this:

(1) Open a fresh terminal window.
(2) Type "echo $SHELL" (without the quotation marks) and press Enter.

This will print something like this:


[steve at sylar ~]$ echo $SHELL
/bin/bash


So I am running "bash" as my shell. So I have the following line in my 
bash login file, .bashrc:


export PYTHONPATH=/home/steve/python/


That creates an environment variable, PYTHONPATH, which Python will 
automatically add to sys.path.

(Other shells will do this differently.)


Another alternative: I also have set a startup file:


export PYTHONSTARTUP=/home/steve/python/startup.py


I can put any Python code I like in startup.py and it will be run just 
before the interactive interpreter.

Or you can follow Peter's advice and use a .pth file.



-- 
Steven



More information about the Tutor mailing list