How can I change the build system of python.sty from Python2 to Python3?
The call to python
is hard-coded in the package. You can make it to call python3
with a patch:
\documentclass{article}
\usepackage{python}
\usepackage{etoolbox}
\patchcmd{\endpython}{python }{python3 }{}{}
\begin{document}
\begin{python}
import sys
print (sys.version)
\end{python}
\end{document}
In the log file we find
runsystem(cat jay.py | python3 > jay.py.out 2> jay.py.err)...executed.
and this is the PDF output
An extended version where you can switch from one version to the other; the \setpython
command obeys the normal scoping rules.
\documentclass{article}
\usepackage{python}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\endpython}{python }{python\python@version\space}{}{}
\newcommand{\setpython}[1]{%
\if3#1\relax
\def\python@version{3}%
\else
\def\python@version{}%
\fi
}
\makeatletter
\setpython{2} % initialize
\begin{document}
\begin{python}
import sys
print (sys.version)
\end{python}
\bigskip
\setpython{3}
\begin{python}
import sys
print (sys.version)
\end{python}
\end{document}