[Tex/LaTex] Python “as” keyword ignored in Listings

listings

Following code cant highlight as keyword. What am I doing wrong?
Shouldn't as be bold like import?

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\lstset{morekeywords={as}}

\title{Python listings}
\author{me}

\begin{document}

\maketitle

\section{Introduction}
Python example

\begin{lstlisting}[language=Python]
#!/usr/bin/env python
"""
An animated image
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
\end{lstlisting}

done with the example.
\end{document}

Best Answer

The morekeyword=as option works if you declare it after telling listings that the language to be used is Python:

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[language=Python,morekeywords=as]
#!/usr/bin/env python
"""
An animated image
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
\end{lstlisting}
\end{document}

enter image description here

It's perhaps better to define an own environment for Python:

\lstnewenvironment{Python}[1][]
  {\lstset{language=Python,
           morekeywords=as,
           #1}%
  }
  {}

so that it will be possible to write

\begin{Python}
<Python code>
\end{Python}

possibly giving other local options, say

\begin{Python}[basicfamily=\ttfamily]
<Python code>
\end{Python}