How to import from freemind files?

26 views
Skip to first unread message

Félix

unread,
Sep 12, 2024, 8:00:41 PM9/12/24
to leo-editor
(All this was tried with the Latest LXML version 5.3.0 )

I tried installing lxml with:

pip install lxml

Seems to have worked, but when importing a freemind file, the imported lxml library does not have the 'html' member, and the import just errors out.

When exploring why the lxml library did not have any valid members, like html or etree as per Leo uses it and was also in the official lxml docs, I tried something :

If I replace the import at the top of the leoImport.py file with :

from lxml import html
instead of:
import lxml

Then, replacing that line further down the file:

htmltree = lxml.html.parse(path)
with this one:
htmltree = html.parse(path)

... makes everything works!!  Wow! :)

I'm a total newb with python so I'm wondering if someone can explain this, and if they have the same behavior experience in Leo with importing FreeMind (.mm or .mm.html files) in leo.

Should I open an issue on github and suggest importing and using it this way?


Thomas Passin

unread,
Sep 12, 2024, 11:51:24 PM9/12/24
to leo-editor
The dotted notation is used for relative imports, meaning relative to a path in sys.path. lxml is installed in my user site-packages directory - there it is at the bottom on my system:

C:\Users\tom>py -c "import sys;print('\n'.join(sys.path))"

C:\Users\tom\AppData\Local\Programs\Python\Python312\python312.zip
C:\Users\tom\AppData\Local\Programs\Python\Python312\DLLs
C:\Users\tom\AppData\Local\Programs\Python\Python312\Lib
C:\Users\tom\AppData\Local\Programs\Python\Python312
C:\Users\tom\AppData\Roaming\Python\Python312\site-packages
C:\Users\tom\AppData\Roaming\Python
C:\Users\tom\AppData\Roaming\Python\Python312\site-packages\win32
C:\Users\tom\AppData\Roaming\Python\Python312\site-packages\win32\lib
C:\Users\tom\AppData\Roaming\Python\Python312\site-packages\Pythonwin
C:\Users\tom\AppData\Local\Programs\Python\Python312\Lib\site-packages



lxml.html means find the lxml package and then the html subpackage relative to it (change paths for your system) starting at

C:\Users\tom\AppData\Local\Programs\Python\Python312\Lib\site-packages.

You can use dotted paths if your import subdirectories are set up right, which they are with lxml (change paths to suit your system) -

import lxml.html
PATH = r"C:\temp\freemind\basic_freemind.xml"

htmltree = lxml.html.parse(PATH)
print(htmltree)


or this -

import lxml.html as html
PATH = r"C:\temp\freemind\basic_freemind.xml"

htmltree = html.parse(PATH)
print(htmltree
)

Read Sec 5.2 of the Python docs at

Thomas Passin

unread,
Sep 13, 2024, 7:25:41 AM9/13/24
to leo-editor
I misread my own printout. The user site-packages directory is the one in the middle:

C:\Users\tom\AppData\Roaming\Python\Python312\site-packages

Reply all
Reply to author
Forward
0 new messages