[Tex/LaTex] FancyVerb Error in using minted package with beamer

beamerfancyvrbminted

Could someone help me with the following error? I am trying to use minted package in beamer.

FancyVerb Error:
File ended while scanning use of \FancyVerbGetLine.

This is a MWE that duplicates the error I get. Thank you.

\documentclass[11pt]{beamer}
\usepackage{minted}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{minted}
\usetheme{AnnArbor}
\begin{document}

\begin{frame}
\frametitle{Test}
\begin{minted}{python}
def first2items(list1):
  return list1[0], list1[1]
a, b = first2items(["Hello", "world", "hi", "universe"])
print a + " " + b
# This is a comment
\end{minted}
\end{frame}

\end{document}

Best Answer

When using verbatim content in beamer, the frame must be fragile:

\documentclass[11pt]{beamer}
\usepackage{minted}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{minted}
\usetheme{AnnArbor}
\begin{document}

\begin{frame}[fragile]
\frametitle{Test}
\begin{minted}{python}
def first2items(list1):
  return list1[0], list1[1]
a, b = first2items(["Hello", "world", "hi", "universe"])
print a + " " + b
# This is a comment
\end{minted}
\end{frame}

\end{document}