I have a fresh install of TeX Live 2013 and am attempting to compile a file using the python package. Here is the relevant portion of the file

print 'hi'

I've compiled successfully on my linux machine with TeX Live 2013, but cannot get my windows 7 machine to compile successfully. When I run latex --shell-escape pytest.tex I get an error which begins with

pytest.py: Forbidden to open for writing
! I can't write on file `pytest.py'.
\@python ...mediate \openout \@out =\@outname .py
                                                   \newlinechar ='15 \begingr...

Can anyone help me fix this so I can compile latex documents using python.sty on my windows machine?


So I'm pretty certain the issue has to do with permissions. If that is the case it would be really great if there were a few easy tests I could run to determine why \write18 cannot open files for writing / what it is trying to do. I've seen some problems from searching that were related to the TEXMFOUTPUT environment variable, but I don't know (1) what to set as the value of TEXMFOUTPUT in order to make sure that these temporary python files are created in the current working directory and (2) if setting TEXMFOUTPUT would have anything to do with these \write18 permission problems.

Ok, one new piece of information: So, I've created a minimal example:


print 'hi'


When compiled after the `! I can't write on file pytest1.py' It says (Press Enter to retry, or Control-Z to exit; default file extension is '.tex' Is this error because write18 can only write to a .tex file?

At the beginning of the compile on the second line it just says ` \write18 enabled'. So it is not running with restricted \write18. I feel like I am really close to fixing this.


The comment python.sty on TeX Live 2013 - Forbidden to open for writing error was exactly right. I had .py listed in my PATHEXT environment variable. Removing it allowed the file to compile without error.

Just read meta.tex.SE and will credit JosephWright once answer is up.

  • 1
    This seems like a problem with permissions. Are you administrator on the machine? Are you running TeX from the command prompt, or from within an editor? Run either (cmd or your editor) as administrator and try again.
    – Werner
    Commented Oct 29, 2013 at 5:44
  • I agree. I am administrator and can either call tex from cygwin's command line or from emacs (C-c C-c). I've tried both and get the same error...
    – Nick H
    Commented Oct 29, 2013 at 5:45
  • 2
    On Windows systems you can't use \write to create any executable file types (.bat is the common one). Do you have .py set up to be associated with Pyhton and runnable? If so, you'll need to write to a different name then use write18 to rename to .py.
    – Joseph Wright
    Commented Oct 29, 2013 at 7:39
  • @JosephWright I believe that python.sty already takes care of the issue of writing executable types. My file is named pytest.tex, when latex compiles it writes to pytest.py, which is then called by python. I won't be back to my home pc until this evening, and will check the .py file associations.
    – Nick H
    Commented Oct 29, 2013 at 14:37
  • @NickH Risky using the same name for all files!
    – Joseph Wright
    Commented Oct 29, 2013 at 15:18

1 Answer 1


On Windows, TeX systems deliberately disable writing of executable file types (which are extension-based) via \write operations. Thus you cannot create a file called foo.py if .py files are set up to be executable. The common case for this problem is batch (.bat) files, which are plain text but executable without any add-ons and thus 'forbidden'. With \write18 enabled, the easiest approach is to write to an alternative file name then use the shell escape to rename the file, which is allowed as \write18 is 'do whatever you want'.

You must log in to answer this question.

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