Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:19:11+00:00 2026-06-04T19:19:11+00:00

I’m attempting to use Pygments and Beautiful Soup as a code highlighting solution for

  • 0

I’m attempting to use Pygments and Beautiful Soup as a code highlighting solution for the blog software I’m building for Google App Engine.

How it works is that my HTML posts will use pre tags to identify code. Like this:

<p>Check out this cool code example<p>    
<pre class="python">
        import this
        def demo():
            pass</pre>

BeautifulSoup captures the part between the pre tag and passes it to Pygments. Pygments is suppose to check the class value and apply the correct lexar. Pygments then applies the formatting and replaces the original text with the formatted text. The solution is explained in more detail at SaltyCrane’s Blog.

The Error

ClassNotFound: no lexer for alias [u'python'] found

Perhaps I just haven’t imported the module correctly?

The code

from google.appengine.ext import db
import fix_path
import bs4
import pygments
from bs4 import BeautifulSoup
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter


    def formatter(p):
        soup = BeautifulSoup(p.otext)
        preblocks = soup.findAll('pre')
        for pre in preblocks:
            if pre.has_key('class'):
                code = ''.join([unicode(item) for item in pre.contents])
                code = unescape_html(code)
                lexer = lexers.get_lexer_by_name(pre['class'])
                formatter = formatters.HtmlFormatter()
                code_hl = highlight(code, lexer, formatter)
                pre.replaceWith(BeautifulSoup(code_hl))
            else:
                print "No Go"
        return unicode(soup)

The traceback

Traceback (most recent call last):
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\john\webdev\workspace\bsby\views.py", line 45, in post
    p.ftext = utils.formatter(p)
  File "C:\Users\john\webdev\workspace\bsby\utils.py", line 18, in formatter
    lexer = lexers.get_lexer_by_name(pre['class'])
  File "C:\Users\john\webdev\workspace\bsby\lib\pygments\lexers\__init__.py", line 80, in get_lexer_by_name
    raise ClassNotFound('no lexer for alias %r found' % _alias)
ClassNotFound: no lexer for alias [u'python'] found
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:19:13+00:00Added an answer on June 4, 2026 at 7:19 pm

    Yep, that was the issue. I had not imported the libraries I needed for the script to run. Here is the correct code.

    import fix_path
    import bs4
    import pygments
    from bs4 import BeautifulSoup
    from pygments import highlight
    from pygments import lexers
    from pygments import formatters
    
    
        def formatter(p):
            soup = BeautifulSoup(p.otext)
            preblocks = soup.findAll('pre')
            for pre in preblocks:
                if pre.has_key('class'):
                    code = ''.join([unicode(item) for item in pre.contents])
                    lexer = lexers.get_lexer_by_name("python")
                    formatter = formatters.HtmlFormatter()
                    code_hl = highlight(code, lexer, formatter)
                    pre.replaceWith(BeautifulSoup(code_hl))
                    return unicode(soup)
                else:
                    return null
    

    The is one other issue….

    I want to pull the lexar name dynamically like this…

    lexer = lexers.get_lexer_by_name(pre['class'])
    

    However, when I do that I get the following error.

    ClassNotFound: no lexer for alias [u'python'] found
    

    If I do

    lexer = lexers.get_lexer_by_name("python")
    

    it works, but only for python.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.