Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install pygments on Ubuntu?

I'm following Django-rest-framework.org tutorial and this is the models.py's code as below.

from django.db import models
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles

LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())

and when i run follow command:

python manage.py syncdb

it gives me this error

ImportError: No module named pygments.lexers

I think that i have to install pygments first to work this code. So tell me how to install pygments in my Ubuntu 12.04. I have Python 2.7 version installed.

like image 986
Jay Modi Avatar asked Oct 06 '14 11:10

Jay Modi


People also ask

How do you install Pygments?

How to Install pygments on Windows? Type "cmd" in the search bar and hit Enter to open the command line. Type “ pip install pygments ” (without quotes) in the command line and hit Enter again. This installs pygments for your default Python installation.

What is Pygments Django?

django-pygments is a Django app that provides a template tag and two filters for doing syntax highlighting with Pygments.


3 Answers

Most basically open a terminal with Ctrl-Alt-t and type sudo apt-get install python-pygments. That will work but there is a better way, which I'll explain.

When you're developing a web app you will eventually want to deploy it. You'll want the environment on which you're developing to be as similar to the one on which you deploy as possible. One way to do this is virtual environments. On Ubuntu you also have the option of Docker.

Virtual environments are probably slightly easier if you're just starting out but I would recommend building up to Docker, which is more complete IMO. If you're using Python 3 then you should use pyvenv to create your virtual environments. On Python 2 you want virtualenv. These will create an isolated Python environment specific to the project they contain, which means you can have many custom setups for many projects, and you can recreate these relatively easily when deploying to production. When the environment is active you can install python packages with pip install package-name-here.

like image 147
cms_mgr Avatar answered Oct 08 '22 17:10

cms_mgr


For Python3, you can install it with sudo apt-get install python3-pygments

As we traditionally use a virtual environment to run Django, you may also want to install it through pip

like image 22
Billal Begueradj Avatar answered Oct 08 '22 19:10

Billal Begueradj


I installed via apt-get install python-pygments , but it didn't solve my ImportError: No module named pygments.

But pip install Pygments solved my problem.

like image 30
HarryQ Avatar answered Oct 08 '22 19:10

HarryQ