How do I define the search path for personal library

L

les_ander

Dear all,
i have a simple question.
Suppose I have my classes such as
myClass1.py
myClass2.py
etc
which I keep in a special folder ~/py_libs

Now
suppose I have a program that is not in py_libs
but I want to do
import myClass1 # note: myClass1 is not in the current directory

how can I set the search path for python so that it can
find myClass1?
thanks
 
P

Peter Hansen

Dear all,
i have a simple question.
Suppose I have my classes such as
myClass1.py
myClass2.py
etc
which I keep in a special folder ~/py_libs

Now
suppose I have a program that is not in py_libs
but I want to do
import myClass1 # note: myClass1 is not in the current directory

how can I set the search path for python so that it can
find myClass1?

There are several options, including the PYTHONPATH environment
variable, creating a .pth file in site-packages or somewhere
(as documented in http://www.python.org/doc/2.4/lib/module-site.html),
or even creating your own sitecustomize.py file (also documented
in the site.py module docs).

What is best depends a bit on your own environment and needs.
For example, do you just want this available at the interactive
prompt, or to all code you write, or to code that other users
on the same system will write?

-Peter
 
R

richardshea

If i understand correctly what you're after I think another option is

import sys
sys.path.append('<your choice of path here')

regards

richard.
 
L

les_ander

Yes, this is exactly what I wanted--just like in perl I can add search
path to @inc.

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,705
Latest member
noname22

Latest Threads

Top