3 import os
, sys
, fnmatch
5 AppDir
= os
.path
.realpath(os
.path
.dirname(__file__
))
9 for root
, dirs
, files
in os
.walk(AppDir
):
11 fpath
= os
.path
.join(root
,fname
)
12 if fnmatch
.fnmatch(fpath
, '*__init__.py'):
13 modules
.append(os
.path
.dirname(os
.path
.dirname(fpath
)))
14 if fnmatch
.fnmatch(fpath
, '*/_*.so'):
15 modules
.append(os
.path
.dirname(fpath
))
16 if fnmatch
.fnmatch(fpath
, '*python*.so'):
17 modules
.append(os
.path
.dirname(fpath
))
18 if fnmatch
.fnmatch(fpath
, '*.py'):
19 modules
.append(os
.path
.dirname(fpath
))
21 modules
= set(modules
)
23 for module
in modules
:
24 sys
.path
.append(module
)
27 E.g., if the main executable is in $APPDIR/usr/bin, then add to the head of it:
30 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
34 if __name__
=="__main__":
36 print("Put this file into the root of an AppDir, then import it to make all Python modules in the AppDir visible to the app")