Cannot import markdown because of COMMAND_LINE_LOG

2019-07-07 04:17发布

问题:

I've got a weird error where I can import markdown in Python, and I can import markdown in python inside the Django runserver, but I get the following when trying to import markdown inside of gunicorn's app server.

* ImportError: cannot import name COMMAND_LINE_LOGGING_LEVEL

This is even more confusing because I found the only place where COMMAND_LINE_LOGGING_LEVEL is referenced in markdown (or any of the code I'm using) -- one line, defining it, in the markdown init.py, and I commented it out. I still get this error.

Any ideas?

回答1:

I fixed this error by removing the .py extension from markdown.py in whatever/bin. This apparently prevented it from importing itself instead of the markdown module in site-packages.



回答2:

Gunicorn, for reasons I don't understand yet, adds the virtualenv/bin directory to the sys.path. Markdown installs a markdown.py into that bin directory. That markdown.py tries to import COMMAND_LINE_LOGGING_LEVEL from markdown the library. This results in a circular failure.

I don't know why Gunicorn does this, and probably it shouldn't. My convenience fix is to add the following to the server's local_settings.py

import sys
for i, path in enumerate(sys.path):
    if path.endswith('bin'):
        del sys.path[i]