Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot run as module #1298

Closed
ervinpopescu opened this issue Oct 28, 2022 · 5 comments
Closed

Cannot run as module #1298

ervinpopescu opened this issue Oct 28, 2022 · 5 comments
Labels
duplicate The issue has been previously reported.

Comments

@ervinpopescu
Copy link

ervinpopescu commented Oct 28, 2022

python -m markdown <filename>.md

fails with:

Traceback (most recent call last):
  File "/usr/lib/python3.10/importlib/util.py", line 96, in find_spec
    parent_path = parent.__path__
AttributeError: module 'html' has no attribute '__path__'. Did you mean: '__name__'?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/lib/python3.10/runpy.py", line 146, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/lib/python3.10/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "/home/ervin/.local/lib/python3.10/site-packages/markdown/__init__.py", line 22, in <module>
    from .core import Markdown, markdown, markdownFromFile
  File "/home/ervin/.local/lib/python3.10/site-packages/markdown/core.py", line 27, in <module>
    from .preprocessors import build_preprocessors
  File "/home/ervin/.local/lib/python3.10/site-packages/markdown/preprocessors.py", line 29, in <module>
    from .htmlparser import HTMLExtractor
  File "/home/ervin/.local/lib/python3.10/site-packages/markdown/htmlparser.py", line 29, in <module>
    spec = importlib.util.find_spec('html.parser')
  File "/usr/lib/python3.10/importlib/util.py", line 98, in find_spec
    raise ModuleNotFoundError(
ModuleNotFoundError: __path__ attribute not found on 'html' while trying to find 'html.parser'

Any ideas?

markdown_py works fine.

Running on Arch currently on linux-next.

@waylan
Copy link
Member

waylan commented Oct 28, 2022

Which version of Python-Markdown are you using? It looks like you are using Python3.10, but is that Arch's default CPython or something else?

It is really odd that it works from markdown_py but not with python -m markdown. What happens when you run from the python console?

@waylan waylan added the more-info-needed More information needs to be provided. label Oct 28, 2022
@ervinpopescu
Copy link
Author

ervinpopescu commented Oct 28, 2022

Python-Markdown version:

$ pip list -v | grep Markdown
Markdown                 3.4.1                  /home/ervin/.local/lib/python3.10/site-packages pip

Python version according to pacman:

$ pacman -Q python
python 3.10.8-2

Running in the python console:

$ python
Python 3.10.8 (main, Oct 13 2022, 21:13:48) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import markdown
Traceback (most recent call last):
  File "/usr/lib/python3.10/importlib/util.py", line 96, in find_spec
    parent_path = parent.__path__
AttributeError: module 'html' has no attribute '__path__'. Did you mean: '__name__'?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ervin/.local/lib/python3.10/site-packages/markdown/__init__.py", line 22, in <module>
    from .core import Markdown, markdown, markdownFromFile
  File "/home/ervin/.local/lib/python3.10/site-packages/markdown/core.py", line 27, in <module>
    from .preprocessors import build_preprocessors
  File "/home/ervin/.local/lib/python3.10/site-packages/markdown/preprocessors.py", line 29, in <module>
    from .htmlparser import HTMLExtractor
  File "/home/ervin/.local/lib/python3.10/site-packages/markdown/htmlparser.py", line 29, in <module>
    spec = importlib.util.find_spec('html.parser')
  File "/usr/lib/python3.10/importlib/util.py", line 98, in find_spec
    raise ModuleNotFoundError(
ModuleNotFoundError: __path__ attribute not found on 'html' while trying to find 'html.parser'
>>>

This is interesting. Following is my pythonrc:

import atexit
import os
import readline
import time
from libqtile.command.client import InteractiveCommandClient

# enable syntax completion
readline.parse_and_bind("tab: complete")

# clear screen
def clear():
  os.system('clear')

c = InteractiveCommandClient()

if 'PYTHONHISTFILE' in os.environ:
  history = os.path.expanduser(os.environ['PYTHONHISTFILE'])
elif 'XDG_DATA_HOME' in os.environ:
    history = os.path.join(os.path.expanduser(os.environ['XDG_DATA_HOME']),
                           'python', 'python_history')
else:
    history = os.path.join(os.path.expanduser('~'),
                           '.python_history')

history = os.path.abspath(history)
_dir, _ = os.path.split(history)
os.makedirs(_dir, exist_ok=True)

try:
    readline.read_history_file(history)
except OSError:
    pass

if readline.get_current_history_length() == 0:
    readline.add_history(f'# History created at {time.asctime()}')

def write_history():
    try:
        readline.write_history_file(history)
    except OSError:
        pass

atexit.register(write_history)

Does this interfere with the module? I'll check by removing it temporarily.
Edit: Nope, it doesn't.

@waylan
Copy link
Member

waylan commented Oct 28, 2022

So the anomaly appears to be markdown_py here. Is it possible that that is using a different Python installed on your system?

Regardless, the code which is causing the error is this:

# Import a copy of the html.parser lib as `htmlparser` so we can monkeypatch it.
# Users can still do `from html import parser` and get the default behavior.
spec = importlib.util.find_spec('html.parser')
htmlparser = importlib.util.module_from_spec(spec)
spec.loader.exec_module(htmlparser)
sys.modules['htmlparser'] = htmlparser

Annoyingly, on some Python installations this seems to fail and I have no idea why as this is the way that importlib is supposed to work. Additionally, I have never been able to replicate the failure myself, which makes debugging the issue nearly impossible.

@ervinpopescu
Copy link
Author

Well, I do have python2 installed, but markdown_py has #!/usr/bin/python as shebang, which is linked to python3, obviously(I use Arch btw xD). This is odd... I'm not sure how to debug this further.

@waylan
Copy link
Member

waylan commented Apr 18, 2023

This appears to be a duplicate of #1132 in that the same root issue is causing the problem.

@waylan waylan closed this as not planned Won't fix, can't repro, duplicate, stale Apr 18, 2023
@waylan waylan added duplicate The issue has been previously reported. and removed more-info-needed More information needs to be provided. labels Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate The issue has been previously reported.
Projects
None yet
Development

No branches or pull requests

2 participants