module search path

Jeff Epler jepler at
Sat Jul 31 18:53:56 CEST 2004

... because ".." is relative to os.getcwd(), and the httpd doesn't
change to the user directory before starting the cgi script?

os.dirname(__file__) should tell you the directory where the current
module resides, as would sys.argv[0]:

    $ cat /tmp/ 
    print __file__
    import sys
    print sys.argv[0]
    $ python /tmp/ 

So you could write something like
    # near the top of index.cgi
    cgidir = dirname(__file__)
    commondir = os.path.join(cgidir, os.pardir, "common")
    import utils
or you might be able to change the the directory where the cgi script
    import common.utils
if you prefer.

Anyway, __file__ is the trick you need to know about.  It is documented:
.. but maybe not where you looked for it, if you even suspected it

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list