# Unable to use python.sty package

When compiling a .tex file that uses python.sty package and contains:

\begin{python}
...
\end{python}


I keep getting an error:

./main.tex:33:I can't find file 'bin/main.py.out' \end{python}

./main.tex:33:Emergency stop \end{python}

This happens both when compiling with PDFLaTeX or only LaTeX. What I tried is:

• ensure -shell-enable is on (or -enable-write18 on Windows),
• find another copy of python.sty in case my version was invalid,
• try on Windows 7 and on Debian 6 with Kile
• copy & paste latex samples using python.sty from the web

None of the above helps in getting rid of that error. It fails even with extremely simple python code within the python environment like: print "a". If also fails when I leave the environment contents empty, but with a different error. It doesn't fail if there are no python environments in the document and there is only \usepackage{python} at the top. My python.sty copy is taken from here. Simple example code I tried to compile is there as well.

What am I missing?

-
Where can one obtain a LaTeX package named python? It doesn't seem to be available through the CTAN. –  Mico Apr 7 '12 at 13:07
Unfortunately it's not available through CTAN. You can get some backup copies, i.e. github.com/silky/latex/tree/master/packages/python. It's supposed to be used to embed python code in latex. –  rook Apr 7 '12 at 13:20

Below is definition of python environment inspired by python.sty package, but rewritten to work on my Windows system. Basically, it takes content from between \begin{python} and \end{python}, writes it to a file, executes the file as python script and inserts output into the LaTeX document. Full code:

\usepackage{verbatim}

\makeatletter
\newwrite\Code@out

\newcommand\python{\obeylines\expandafter\pythonArg\noexpand}

\newcommand\pythonArg[1][tmp.py.in]{%
\gdef\FNameIn{#1}
\gdef\FNameOut{tmp.py.out}
\begingroup
\@bsphack%
\immediate\openout\Code@out\FNameIn%
\let\do\@makeother\dospecials%
\catcode\^^M\active%
\def\verbatim@processline{%
\immediate\write\Code@out{\the\verbatim@line}}%
\verbatim@start}

\def\endpython{%
\immediate\closeout\Code@out\@esphack
\endgroup

%Execute python script. Python directory must be in PATH.
\immediate\write18{python \FNameIn > \FNameOut}
\input{\FNameOut}
}

\makeatother

-

It works fine here. Below is the output from the example included in the README file. I did remove the bin/ directory so that I would not need to create that folder. Perhaps you could try that as well. Alternatively, try creating a "bin/" directory where your LaTeX file is.

-
Thank you for taking time to investigate it. It didn't fix the issue, though. Did it create a main.py.out` file anywhere for you? –  rook Apr 7 '12 at 13:36
I had saved the sample as README.tex (and edited it to remove the non-LaTeX content) and it created various README... files in the same directory where the .tex file was located including README.py.out. If you are on Windows, I know that there are sometimes problems with default path to find Python. Perhaps you can investigate this... I might be able to find some links for you to look at if you don't know what I mean. –  André Apr 7 '12 at 13:48
If you mean adding python's directory to PATH environment variable - it's already done. –  rook Apr 7 '12 at 13:54
Perhaps try also adding PYTHONPATH as an environment variable ... and perhaps "." as another path variable... –  André Apr 7 '12 at 15:27
I'm only wondering what directories should be included in PYTHONPATH then. Location of the .tex file? May I ask what python version and architecture are you running? –  rook Apr 7 '12 at 15:55