Source

pytest / _findpy.py

#!/usr/bin/env python 

#
# try to find and import a nearby version of the 'py' package. 
# otherwise use the system global default 
# XXX turn this into a developer-only thing? 
#
import sys
import os
from os.path import dirname as opd, exists, join, basename, abspath

def searchpy(current):
    while 1:
        last = current
        initpy = join(current, '__init__.py')
        if not exists(initpy):
            pydir = join(current, 'py')
            # recognize py-package and ensure it is importable
            if exists(pydir) and exists(join(pydir, '__init__.py')):
                #for p in sys.path:
                #    if p == current:
                #        return True
                if current != sys.path[0]:  # if we are already first, then ok
                    print >>sys.stderr, "inserting into sys.path:", current
                    sys.path.insert(0, current)
                return True
        current = opd(current)
        if last == current:
            return False

if not searchpy(abspath(os.curdir)):
    if not searchpy(opd(abspath(sys.argv[0]))):
        if not searchpy(opd(__file__)):
            pass # let's hope it is just on sys.path 

import py

if __name__ == '__main__': 
    print "py lib is at", py.__file__
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.