TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
what does python -v on the command line say? – yo' 5 hours ago
    
@yo' It should be -V :P On my OS, python is version 2.7.9 while python3 is version 3.5. – Jay Wong 5 hours ago
up vote 4 down vote accepted

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

share|improve this answer
    
Thank you, this way is so cool! You are replacing the python with python3 in a higher level. – Jay Wong 4 hours ago
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 4 hours ago
    
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 Wong 3 hours ago

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.