By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Appending paths relative to the current file to sys.path

P: n/a
Out of interest, are there any standard Python modules that do this:

def appendRelativeIncludePath(*relpath):
dir = os.path.abspath(os.path.join(os.path.dirname(__fil e__), *relpath))
if not dir in sys.path:
sys.path.append(dir)

I ask because I often find myself doing this:

----
# myproject/lib/mymodule/test/test.py

if __name__ == '__main__':
import os.path
import sys

dir = os.path.abspath(os.path.join(os.path.dirname(__fil e__), '..',
'..'))
if not dir in sys.path:
sys.path.append(dir)

import mymodule # it's always 2 directories up from here

----

And it seems like a lot to type so often. I can't factor out this
functionality unless I create a little module for doing this and install
it in a standard include path. You see I dream of doing this:

----
# myproject/lib/mymodule/test/test.py

if __name__ == '__main__':
import astandardmodule
astandardmodule.appendRelativeIncludePath('..', '..')
import mymodule

----

Which, as you can see, is much shorter. ;)

--
Greg McIntyre
Sep 12 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.