8

I am using the python.sty package, downloaded here. I found the default build path is to use Python2.7:

\documentclass{article}
\usepackage{python}
\begin{document}
\begin{python}%
import sys
print (sys.version)
\end{python}%
\end{document}

This tex gave me 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

My question is can I change the Python version, which python.sty is using ,to Python 3.5?

2
  • what does python -v on the command line say?
    – yo'
    Jun 17, 2016 at 20:47
  • @yo' It should be -V :P On my OS, python is version 2.7.9 while python3 is version 3.5.
    – Jay Wang
    Jun 17, 2016 at 20:53

2 Answers 2

5

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

enter image description here

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}

enter image description here

3
  • Thank you, this way is so cool! You are replacing the python with python3 in a higher level.
    – Jay Wang
    Jun 17, 2016 at 21:34
  • 1
    @JayWong The extended version is even better, as it allows you to switch between the two versions in the same document. You may also consider PythonTeX, for many more functionalities (but requires a separate call of pythontex).
    – egreg
    Jun 17, 2016 at 21:35
  • Yeah, I am reading some examples of pythontex. It looks really powerful, and it has already been installed. Hmm but currently, your solution is totally enough for my project.
    – Jay Wang
    Jun 17, 2016 at 22:10
1

Make a directory in TEXMFHOME for your adapted version:

mkdir -p $(kpsewhich -var TEXMFHOME)/tex/latex/python3/

Move there

pushd $(kpsewhich -var TEXMFHOME)/tex/latex/python3/
sed -e 's+\(ProvidesPackage{python\)\(}\[\).*\(.*Python in LaTeX\)+\13\22016/06/17 v0.21-3 \3 modified for Python3 by JW+' -e 's+\(\\immediate\\write18{.*\| python\)\( > \\jobname\.py\)+\13\2+' $(kpsewhich python.sty) > python3.sty
popd

and then say

\usepackage{python3}

Note that this is entirely untested - I know as close to zilch about Python as makes no odds, but python on my system just is 3.5.1.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .