You need to put the pygments module in web2py/site-package or (if you
use the web2py source distribution) install it normally under python
with

easy_install pygments

Some modules can be installed under applications/yourapp/modules/ but
not all. The reason for this special folder is to allow apps to
include some modules and bypass the python sys.path import mechanism
to avoid conflicts with existing modules. For this we use local_import
instead of import. This works only if the module being imported itself
does not rely on sys.path. Pygments does. It assumes there is only one
instance of pygments in a location listed in sys.path. This prevents
two apps within web2py to have two different copies of pygments and
breaks the import mechanism.

Anyway. web2py has syntax highliting:

{{=CODE(code,language='html')}}

language can be 'web2py','html','python','cpp' and 'java'. It is not
as flexible as pygments and has less options but gets the job done.



On Feb 18, 11:45 am, leeubill <leeub...@gmail.com> wrote:
> Hello all,
>
> Have just joined this group, and this is my first post. I have
> actually been following web2py since year 2009. At that time, I was
> deciding which language and which webapp framework to use. Web2py is
> the one I have chosen and I have been spending a lot of time on it.
> This is my first question here, since I can't find the answer/
> confirmation in Google search.
>
> I am trying to add Pygments script as part of my webapp so that its
> functionality can be called in my controllers.
> First of all I try adding the pygments folder to my specific app's /
> modules folder, and do a local_import('pygments', reload=True) in my
> models/db.py file.
>
> Following that, I put in this sample code (the sample expected output
> as per the pygments quickstart page.)
> (http://pygments.org/docs/quickstart/)
>
> from pygments import highlight
> from pygments.lexers import PythonLexer
> from pygments.formatters import HtmlFormatter
>
> code = 'print "Hello World"'
> print highlight(code, PythonLexer(), HtmlFormatter())
>
> I get a couple of errors here.
> 1. Inside the pygments' folder original __init__.py file , there is
> this line>> from pygments.util import StringIO, BytesIO
>
> ImportError: No module named pygments.util
>
> Here, the reference to "pygments.util" will cause an error? Which I
> solved by renaming it to simply "util".
> But on to next line...
>
> 2. After doing that, I get an error on the next lines,>>from pygments.lexers 
> import PythonLexer
>
> 'module' object has no attribute 'lexers'.
>
> Eventually I got everything working by giving up on putting pygments
> in modules/ -- as I intended to ,
> and instead (reluctantly) put it in the web2pySource's /site-packages
> folder.
>
> Q. My question is -- does this mean that code placed in the modules/
> folder and imported through 'local_import', is not actually able to be
> 100% equivalent as having the original script installed in the python
> environment(not an option for me) or putting it as a global web2py
> site-package?
>
> Just want to confirm that there is a difference in putting a script
> package in app/modules compared to putting it in the global web2py
> source site-packages. Thanks.

Reply via email to