简体   繁体   中英

Pyinstaller and Python-Markdown - ImportError: no module named 'extra'

I'm running into an issue trying to get python-markdown to work in pyinstaller. I have this code to demonstrate the issue in file called test.py:

import markdown

testMarkdown = "blahdy blah blah"
print(markdown.markdown(testMarkdown))
print(markdown.markdown(testMarkdown, extensions=["extra"]))

When I run it using python3, I get as desired:

(venv) C:\Users\madgrizzle>python3 test.py
<p>blahdy blah blah</p>
<p>blahdy blah blah</p>

I run pyinstaller as follows:

(venv) C:\Users\madgrizzle>pyinstaller test.py

and run the resulting code, I get the following:

(venv) C:\Users\madgrizzle\dist\test>test
<p>blahdy blah blah</p>
Traceback (most recent call last):
  File "test.py", line 5, in <module>
  File "lib\site-packages\markdown\core.py", line 390, in markdown
  File "lib\site-packages\markdown\core.py", line 100, in __init__
  File "lib\site-packages\markdown\core.py", line 126, in registerExtensions
  File "lib\site-packages\markdown\core.py", line 166, in build_extension
  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 985, in _gcd_import
  File "<frozen importlib._bootstrap>", line 968, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
ImportError: No module named 'extra'
[14432] Failed to execute script test

I tried to rebuild using:

(venv) C:\Users\madgrizzle>pyinstaller --hidden-import="markdown.extensions.extra" test.py

but I get the same error message.

Is there something special that's needed for including markdown extensions?

Additional Information:

It appears that the 'extra' extension might be causing the problem. Per https://python-markdown.github.io/extensions/extra/ , 'extra' is a compilation of multiple extensions, including fenced_code and tables. If I just try to use the tables extension by itself, pyinstaller works IF I use the full-name as follows:

markdown.markdown(testMarkdown, extensions=["markdown.extensions.tables"])

If instead of using 'markdown.extensions.tables' I use 'markdown.extensions.extra', compile using pyinstaller, and run it, it responds back with a missing "fenced_code" module. Basically, it seems I have to avoid 'extra' with pyinstaller.

Short names for extensions like extra and table are setuptools entrypoints. I expect that pyinstaller does not play nice with entrypoints. Therefore, you need to use the full importable string name for all extensions.

However, as extra uses the short names internally, that means you cannot use extra . You will need to call each of the nested extensions separately by their full importable string name:

markdown.markdown(
    testMarkdown, 
    extensions=[
        "markdown.extensions.abbr", 
        "markdown.extensions.attr_list", 
        "markdown.extensions.def_list", 
        "markdown.extensions.fenced_code", 
        "markdown.extensions.footnotes", 
        "markdown.extensions.tables"
    ]
)

The only problem with that is that you won't get any extra specific behavior. However, when version 3.2 is released, the changes here will be available and you can add markdown.extensions.md_in_html to the list of extensions. At that point, you will get all of extra without needing to include extra at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM