Pdflatex in python subprocess on mac

I am trying to run pdflatex in a .tex file from Python 2.4.4. subprocess (on Mac):

import subprocess subprocess.Popen(["pdflatex", "fullpathtotexfile"], shell=True) 

which actually does nothing. However, I can run "pdflatex fullpathtotexfile" in the terminal without problems, creating pdf. What am I missing?

[EDIT] As suggested in one of the answers, I tried:

 return_value = subprocess.call(['pdflatex', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False) 

which fails:

 Traceback (most recent call last): File "/Users/Benjamin/Desktop/directory/generate_tex_files_v3.py", line 285, in -toplevel- return_value = subprocess.call(['pdflatex', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False) File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py", line 413, in call return Popen(*args, **kwargs).wait() File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py", line 543, in __init__ errread, errwrite) File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py", line 975, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory 

The file exists and I can run pdflatex /Users/Benjamin/Desktop/directory/ON.tex in the terminal. Note that pdflatex does generate a large number of warnings ... but it does not matter, and it also gives the same error:

 return_value = subprocess.call(['pdflatex', '-interaction=batchmode', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False) 
+4
source share
2 answers

Use the convenience function, subprocess.call

You do not need to use Popen here, a call should be enough.

For instance:

 >>> import subprocess >>> return_value = subprocess.call(['pdflatex', 'textfile'], shell=False) # shell should be set to False 

If the call was successful, return_value will be set to 0 or 1.

Using Popen usually done when you want the repository to be displayed. For example, you want to check the kernel release using the uname command and save it in some variable:

 >>> process = subprocess.Popen(['uname', '-r'], shell=False, stdout=subprocess.PIPE) >>> output = process.communicate()[0] >>> output '2.6.35-22-generic\n' 

Again, never set shell=True .

+3
source

You may need:

 output = Popen(["pdflatex", "fullpathtotexfile"], stdout=PIPE).communicate()[0] print output 

or

 p = subprocess.Popen(["pdflatex" + " fullpathtotexfile"], shell=True) sts = os.waitpid(p.pid, 0)[1] 

(The doc subprocess is shamelessly torn from this section).

+1
source

All Articles