Skip to content

Instantly share code, notes, and snippets.

@maphew
Last active February 28, 2019 06:37
Show Gist options
  • Save maphew/c4670b3cb6e045e7583769de6a185ee6 to your computer and use it in GitHub Desktop.
Save maphew/c4670b3cb6e045e7583769de6a185ee6 to your computer and use it in GitHub Desktop.
Snippets for finding Leo's library path (ie: ./leo-editor/leo or PYTHONHOME/Lib/site-packages/leo)
# import pdb ; pdb = pdb.set_trace
import os
import sys
# Partial fix for #541.
# See https://stackoverflow.com/questions/24835155/pyw-and-pythonw-does-not-run-under-windows-7/30310192#30310192
if sys.executable.endswith("pythonw.exe"):
sys.stdout = open(os.devnull, "w");
sys.stderr = open(
os.path.join(os.getenv("TEMP"),
"stderr-"+os.path.basename(sys.argv[0])),
"w")
path = os.getcwd()
if path not in sys.path:
# print('appending %s to sys.path' % path)
sys.path.append(path)
# Import leoGlobals, but do NOT set g.
import leo.core.leoGlobals as leoGlobals
# Create g.app.
import leo.core.leoApp as leoApp
leoGlobals.app = leoApp.LeoApp()
# **Now** we can set g.
g = leoGlobals
assert(g.app)
# --- this creates a traceback ---
g.es_print(g.computeLeoDir())
r'''
Result:
(leo-dev) matt@ENV-SURFACE-MHW C:\Users\mattw\code\leo-editor
> python find-leo-lib-using-leo.py
Traceback (most recent call last):
File "find-leo-lib-using-leo.py", line 26, in <module>
g.es_print(g.computeLeoDir())
File "C:\Users\mattw\code\leo-editor\leo\core\leoGlobals.py", line 3409, in computeLeoDir
return g.app.loadManager.computeLeoDir()
AttributeError: 'NoneType' object has no attribute 'computeLeoDir'
'''
# simple and works, but is there a reason not to I don't know about?
import leo
leolibpath = leo.__path__
print(leolibpath)
r'''
Result:
(leo-dev) matt@ENV-SURFACE-MHW C:\Users\mattw\code\leo-editor
> python find-leo-lib.py
['C:\\Users\\mattw\\code\\leo-editor\\leo']
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment