Thursday, 15 January 2015

python - Get package root and full module name from .py file for importing -



python - Get package root and full module name from .py file for importing -

i'm looking simple , fast way find root of bundle , total module name path .py file.

i want user take .py , import without breaking. importing module if it's part of bundle break. want automatically append directory wherein root of bundle resides sys.path (if not in there) , import module total module name.

i'm not running anywhere same directory or script, can't utilize __file__ , kind of thing. don't have module imported yet, can't (as far know) inspect module object, because there none.

this working version, yet i'm interested in finding easier/faster solutions.

def splitpathfull(path): folders=[] while 1: path,folder=os.path.split(path) if folder!="": folders.append(folder) else: if path!="": folders.append(path) break folders.reverse() homecoming folders def getpackagerootandmodulenamefromfilepath(filepath): """ recursively looks until finds folder without __init__.py , uses root of bundle root of package. """ folder = os.path.dirname(filepath) if not os.path.exists( folder ): raise runtimeerror( "location not exist: {0}".format(folder) ) if not filepath.endswith(".py"): homecoming none modulename = os.path.splitext( os.path.basename(filepath) )[0] # filename without extension # # if there's __init__.py in folder: # find root module folder recursively going until there's no more __init__.py # else: # it's standalone module/python script. # foundscriptroot = false fullmodulename = none rootpackagepath = none if not os.path.exists( os.path.join(folder, "__init__.py" ) ): rootpackagepath = folder fullmodulename = modulename foundscriptroot = true # it's not in python bundle seperate ".py" script # append it's directory name sys path (if not in there) , import .py module else: startfolder = folder modulelist = [] if modulename != "__init__": modulelist.append(modulename) amountup = 0 while os.path.exists( folder ) , foundscriptroot == false: modulelist.append ( os.path.basename(folder) ) folder = os.path.dirname(folder) amountup += 1 if not os.path.exists( os.path.join(folder, "__init__.py" ) ): foundscriptroot = true splitpath = splitpathfull(startfolder) rootpackagepath = os.path.join( *splitpath[:-amountup] ) modulelist.reverse() fullmodulename = ".".join(modulelist) if fullmodulename == none or rootpackagepath == none or foundscriptroot == false: raise runtimeerror( "couldn't resolve python bundle root python path , total module name for: {0}".format(filepath) ) homecoming [rootpackagepath, fullmodulename] def importmodulefromfilepath(filepath, reloadmodule=true): """ imports module it's filepath. adds root folder sys.path if it's not in there. imports module total package/module name , returns imported module object. """ rootpythonpath, fullmodulename = getpackagerootandmodulenamefromfilepath(filepath) # append rootpythonpath sys.path if not in sys.path if rootpythonpath not in sys.path: sys.path.append(rootpythonpath) # import total (module.module.package) name mod = __import__( fullmodulename, {}, {}, [fullmodulename] ) if reloadmodule: reload(mod) homecoming mod

this impossible due namespace packages - there no way decide whether right bundle baz.py foo.bar or bar next file structure:

foo/ bar/ __init__.py baz.py

python import module package

No comments:

Post a Comment