[Tex/LaTex] Problem using python package with MiKTeX

errorspythonwindows

When I try to compile the following file, test.tex, in an otherwise empty folder:

\documentclass[]{report}

\usepackage{python}

\begin{document}

\begin{python}
print(0)
\end{python}

\end{document}

I get the following error message:

! I can't write on file `test.py'.

What is the reason for this; why can't pdflatex write on that file?

I have tried to compile from the TeXstudio editor, as well as from Command Prompt and from Git Bash, both as my own user and in elevated mode as administrator, always getting the same error. I'm compiling with pdflatex.exe -synctex=1 -interaction=nonstopmode -shell-escape test.tex from the terminal, and with pdflatex.exe -synctex=1 -interaction=nonstopmode -shell-escape %.tex from TeXstudion, which should generate the same command. I'm using MiKTeX and Windows 10. My Python version is 3.5.3 and it is 64-bit.

Here is the entire log file, test.log:

This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6350 64-bit) (preloaded format=pdflatex 2017.7.26)  2 NOV 2017 17:19
entering extended mode
**./test.tex
(test.tex
LaTeX2e <2017-04-15>
Babel <3.10> and hyphenation patterns for 75 language(s) loaded.
("D:\Program Files\MiKTeX 2.9\tex\latex\base\report.cls"
Document Class: report 2014/09/29 v1.4h Standard LaTeX document class
("D:\Program Files\MiKTeX 2.9\tex\latex\base\size10.clo"
File: size10.clo 2014/09/29 v1.4h Standard LaTeX file (size option)
)
\c@part=\count79
\c@chapter=\count80
\c@section=\count81
\c@subsection=\count82
\c@subsubsection=\count83
\c@paragraph=\count84
\c@subparagraph=\count85
\c@figure=\count86
\c@table=\count87
\abovecaptionskip=\skip41
\belowcaptionskip=\skip42
\bibindent=\dimen102
)
("D:\Program Files\MiKTeX 2.9\tex\latex\python\python.sty"
Package: python 2007/06/07 v0.21 Python in LaTeX
\@out=\write3
\@module=\write4
)
No file test.aux.
\openout1 = `test.aux'.

LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 5.
LaTeX Font Info:    ... okay on input line 5.
! I can't write on file `test.py'.
\@python ...mmediate \openout \@out =\jobname .py 
                                                  \newlinechar ='15 \begingr...
l.8 p
     rint(0)
Please type another output file name
! Emergency stop.
\@python ...mmediate \openout \@out =\jobname .py 
                                                  \newlinechar ='15 \begingr...
l.8 p
     rint(0)
*** (job aborted, file error in nonstop mode)


Here is how much of TeX's memory you used:
 231 strings out of 493323
 2485 string characters out of 3139054
 53648 words of memory out of 3000000
 3856 multiletter control sequences out of 15000+200000
 3640 words of font info for 14 fonts, out of 3000000 for 9000
 1141 hyphenation exceptions out of 8191
 23i,0n,17p,118b,36s stack positions out of 5000i,500n,10000p,200000b,50000s
!  ==> Fatal error occurred, no output PDF file produced!

Except from this log file, I also get a file called test.aux, but that's it. There is no file called test.py that is generated.

Best Answer

If pdflatex can't write the py file then the most probable cause is some protection software that denies it the rights. There has been some reports that e.g. bitdefender does this: pdflatex won't write in certain folders

But this is perhaps not the sole problem that you will get with the python package. It uses the program cat to write the files which normally doesn't exist in windows. I only could compile your file in a git bash.

Edit

I think I found the reason why on some systems the .py can not be written. On the one computer where it failed for me too .PY was in the PATHEXT variable:

 PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY

After I removed .PY (this needed admin rights) pdflatex compiled again and could write the .py-file.