Appending paths relative to the current file to sys.path

Discussion in 'Python' started by Greg McIntyre, Sep 12, 2005.

  1. 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(__file__), *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(__file__), '..',
    '..'))
    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
     
    Greg McIntyre, Sep 12, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    409
  2. Amir Dekel
    Replies:
    9
    Views:
    3,930
    Amir Dekel
    Dec 17, 2004
  3. mhearne808[insert-at-sign-here]gmail[insert-dot-he

    Appending to sys.path

    mhearne808[insert-at-sign-here]gmail[insert-dot-he, Mar 24, 2009, in forum: Python
    Replies:
    1
    Views:
    394
    Peter Otten
    Mar 24, 2009
  4. Darren Hart
    Replies:
    0
    Views:
    223
    Darren Hart
    Oct 30, 2011
  5. Peng Yu
    Replies:
    3
    Views:
    744
    Peng Yu
    Mar 18, 2013
Loading...

Share This Page