sys.path and importing modules from other directories

Discussion in 'Python' started by Martin P. Hellwig, Apr 6, 2008.

  1. Hello all,

    I had some troubles in the past how to arrange my packages and modules,
    because I usually don't develop my stuff in the Lib\site-packages
    directory I have some troubles when importing depending modules that are
    in 'sibling' directories. Like in the following scenario:
    pkg_root\
    -__init__
    - common\
    - - __init__
    - - something
    - other\
    - - __init__
    - - working_on

    So when I am busy with 'working_on' and want to import from common
    something it won't let me do that because I don't have pkg_root in my
    path. No biggy here, I just add the pkg_root to the path. Once I am
    finished most of the time all modules are initiated from a module
    directly at the pkg_root level so then importing works without worries.

    However it has irritated me enough that I wrote a little script I called
    'relative_path.py' that I put in my python path that does these things
    for me, so that I only need to import that one and set the parent path,
    probably I missed a point here somewhere but if not maybe somebody else
    finds it useful.

    So while I am developing the depending modules, these modules begin with:
    import relative_path
    relative_path.add_package_root()


    +++
    import os, sys

    def getparent(depth=0):
    "Returns the absolute pathname of (grand)parents directory."
    # By default it returns the current working directory.
    # The level of 'grand' is set by depth.

    getcwd = os.getcwd
    sep = os.sep
    abspath = os.path.abspath
    if depth == 0:
    grand = None
    else:
    grand = depth * -1

    # Main function
    # From the current working directory get the absolute path,
    # split it and join the wanted portion of the list.
    pathname = sep.join(abspath(getcwd()).split(sep)[:grand])

    # Make sure to throw an exception if depth results in an empty string.
    if len(pathname) == 0:
    raise_string = "(grand)Parent depth of %s is before root." % depth
    raise(LookupError(raise_string))
    else:
    return(pathname)


    def add_package_root(directory=1):
    "Add a parent directory relative to cwd to sys.path, default the
    first."
    sys.path.append(getparent(directory))
    +++

    --
    mph
     
    Martin P. Hellwig, Apr 6, 2008
    #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. Remy Cool
    Replies:
    1
    Views:
    433
    Remy Cool
    Aug 27, 2003
  2. Tobiah
    Replies:
    2
    Views:
    312
    Tobiah
    Sep 14, 2003
  3. Replies:
    0
    Views:
    491
  4. Alexandru  Moșoi

    importing modules from alternate path

    Alexandru Moșoi, Mar 24, 2009, in forum: Python
    Replies:
    1
    Views:
    323
    R. David Murray
    Mar 24, 2009
  5. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Replies:
    0
    Views:
    301
    Peter Peyman Puk
    Mar 19, 2010
Loading...

Share This Page