[py-dev] Local import

Joan Miller peloko45 at gmail.com
Wed Dec 23 14:28:14 CET 2009


I'm using the next function to access to the source from tests without
install it (python setup.py develop). It's very usefull during
development so it could be added to py.


------------------------
import os
import sys

def import_local(parent='test', child='lib'):
    """Inserts local library on 'sys.path'.

    Goes up until the root of the project (upper of 'parent' directory)
    and inserts the 'child' directory.
    """
    parent_path = os.path.abspath(os.path.dirname(__file__))

    while parent in os.path.basename(parent_path):
        parent_path = os.path.dirname(parent_path)

    sys.path.insert(0, os.path.join(parent_path, child))
------------------------



More information about the Pytest-dev mailing list