Python code in LaTeX not running

I managed, thankfully with your help, to run python code in LaTeX. Unfortunately the code below is not running (simple commands like print "hi" are running).

Could anybody tell me, what the problem might be? I am using a Mac.

% !TEX program = XeLaTeX+shell-escape
\documentclass{article}
\usepackage{grffile}
% Support for PDF inclusion
\usepackage[final]{pdfpages}
\usepackage{hyperref}
\usepackage{python}

\begin{document}

\tableofcontents
\clearpage

\begin{python}%
import os
from Tkinter import Tk

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing

# show an "Open" dialog box and return the path to the selected file

# get filenames from path

for name in v5:

# write latex code
for x in v5:
if not x.startswith("."): #versteckte Systemdateien ausblenden
print "\phantomsection"
print "\\addcontentsline{toc}{section}{" + x[:-12] + "}"
else:
print "\\addcontentsline{toc}{section}{" + x[:-4] + "}"
print "\includepdf[fitpaper=true]{" + Liederpfad + "/" + x + "}"
print ""
\end{python}%

\end{document}


I tried as well a smaller program which is not running as well (I was not sure if there where some special characters in the code which might have caused the problem...

% !TEX program = XeLaTeX+shell-escape
\documentclass{article}
\usepackage{python}

\begin{document}

\begin{python}%
import os
v2 = os.path.abspath(".")
v4 = os.listdir(v2)
for name in v4:
print name
\end{python}%

\end{document}


This was the result of the code above in Python:

.DS_Store
1.pdf
latex.py
lieder
Liederbuch.aux
Liederbuch.log
Liederbuch.out
Liederbuch.pdf
Liederbuch.py
Liederbuch.py.err
Liederbuch.py.out
Liederbuch.tex
Liederbuch.toc
pyth.py
Songbuk.py
test.py


Hope anybody can help me.

-
Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. –  Martin Schröder Oct 6 '13 at 19:58

In your short example, you are trying to print file names. But in general, file names will not be valid LaTeX, since they may contain underscores, etc. If you do something like

print('\\verb|' + name + '|\n')


then the file names will be wrapped in a \verb command and thus special characters won't be an issue.

Also, you don't need the % after the python environment.

Depending on what you ultimately want to do, you may wish to consider the pythontex package. It provides a lot of additional features, but also involves a three-step compile process (LaTeX, pythontex.py, and then LaTeX again)

-
Pythontex is definitely the best way to integrate python and TeX. Can one in principle with LuaTeX avoid the three-step process by calling python from Lua's os module? –  Alexander Oct 6 '13 at 22:09
@Alexander The 3-step process allows code from multiple environments/commands to be collected and then executed in a single session. In principle, I believe it would be possible to do a single-step process using LuaTeX. Each bit of Python code would be checked to see if it was modified. If not, a cached version of output would be loaded; otherwise, all the preceding code in that session plus the current code would have to be executed, and the session would have to be available for later code (some sort of server?). It should be possible, but it would be complex. –  G. Poore Oct 7 '13 at 0:21
@Alexander On further thought, a LuaTeX version would actually be slower under some circumstances. In the current approach, all modified Python code is executed at once (step 2 of the process), and sessions run in parallel. A LuaTeX version would have to run all Python code in serial as the document was processed. If only a single session is modified, a LuaTeX version would save the time of one TeX compile. If multiple sessions must be executed, the current approach would be significantly faster in some cases. –  G. Poore Oct 7 '13 at 0:31
@G.Poore Thanks a lot! When I tried the pythontex package with a simple example it only produced "??" in the output. What could be the problem? Thanks! \documentclass[12pt]{scrartcl} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage{pythontex} \begin{document} \py{2+2} \end{document} –  Andreas Oct 10 '13 at 19:15
@Andreas The ?? is a placeholder until code is actually executed. Unlike the python package, pythontex doesn't use \write18 to run code during document compilation. Compiling a document with pythontex is a three-step process: run LaTeX, then run pythontex.py <file>.tex, then run LaTeX again. –  G. Poore Oct 10 '13 at 21:40