argument in newenvironment

I am trying to write a newenvironment that would be a wrapper for code layout, using minted.

So far, I tried that:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{minted}

\newenvironment{codeblock}[1][python]{%
\minted[linenos,frame=lines]{#1}%
}{%
\end{minted}%
}

\begin{document}

\begin{codeblock}[python]
print('Hello world')
\end{codeblock}

\end{document}

But I get the following error:

! Illegal parameter number in definition of \reserved@a.

I'm not sure to get what's wrong; I read some stuff about passing argument to macro that can be tricky, but I'm not sure to get exactly how it works.

Any help would be appreciated!

• Look at section 7 of the manual for minted and \newmint. It has several limitations, however. Note that your attempt cannot work and is not really fixable. – egreg Jun 18 '15 at 9:22
• minted like any verbatim environment can not easily be embedded in another macro, it turns off all normal tex parsing so \end is not seen as the usual latex end command, instead verbatim environments look for a specific end string \end{minted} here and that is not seen. minted may have a way of being embedded, you'd need to check its documentation but it is all about verbatim environments not a general issue of argument passing. – David Carlisle Jun 18 '15 at 9:22

The minted package just offers the \newminted facility, which has several limitations.

You can profit of the powerful tcolorbox package, instead:

\documentclass{article}

\usepackage[most]{tcolorbox}
\tcbuselibrary{minted}

\NewTCBListing{codeblock}{O{python}}{%
%colback=white,colframe=white,
listing only,
minted language=#1,
}

\begin{document}

\begin{codeblock}[python]
print('Hello world')
\end{codeblock}

\end{document}

Look at the package documentation for customizing the code block appearance.