As part of larger code, I am trying to make a function that calls to a latexmk compiler using subprocess, but I consistently get FileNotFoundError: [Errno 2] No such file or directory: 'latexmk': 'latexmk'
However, If I write the command directly in the terminal, everything works: latexmk --pdf test.tex
In case it is important, I am on MacOS Mojave 10.14.6, running python 3.6 spyder through anaconda
I have checked the following links:
https://askubuntu.com/questions/801493/python-subprocess-call-not-working-as-expected
OSError: [Errno 2] No such file or directory while using python subprocess in Django
Running Bash commands in Python
If anything there solves the problem, I missed it.
To make everyone's life easier, here's a link to a .tex file [you can use your own]:
https://drive.google.com/open?id=1DoJnvg2BmbRCzmRmqFYRVybyTQUtyS-h
Afer putting type latexmk to terminal it outputs:
latexmk is hashed (/Library/TeX/texbin/latexmk)
Here is the minimal reproducible example (you do need latexmk on your computer though):
import os, subprocess
def pdf(file_path):
cur_dir = os.getcwd()
dest_dir = os.path.dirname(file_path)
basename = os.path.basename(file_path)
os.chdir(dest_dir)
main_arg = [basename]
command = ["latexmk", "--pdf"] + main_arg
try:
output = subprocess.check_output(command)
except subprocess.CalledProcessError as e:
print(e.output.decode())
raise
os.chdir(cur_dir)
pdf("path to your .tex file")
I have a feeling that I am grossly misunderstanding the way subprocess works. Any ideas?
Update: In case neccessary, the full traceback:
Traceback (most recent call last):
File "<ipython-input-90-341a2810ccbf>", line 1, in <module>
pdf('/Users/sergejczan/Desktop/untitled folder/test.tex')
File "/Users/sergejczan/Desktop/Lab/subprocess error reproduction.py", line 23, in pdf
output = subprocess.check_output(command)
File "/anaconda3/lib/python3.6/subprocess.py", line 336, in check_output
**kwargs).stdout
File "/anaconda3/lib/python3.6/subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "/anaconda3/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'latexmk': 'latexmk'
New Update
Changing the output = subprocess.check_output(command) line with the hardcoded envirnoment that I got from echo $PATH worked wonderfully.
output = subprocess.check_output(command,env = {'PATH': '/anaconda3/bin:/Users/sergejczan/anaconda3/bin:/Users/sergejczan/Desktop/Lab/anaconda2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin'})
Would you think that there is a way to make the code find the PATH automatically?
Related
i get the error bellow, running the python code,
im running the program through anaconda cause it works only with python 3.6 or something, but i get the following error.
Traceback (most recent call last):
File "inference.py", line 279, in
main()
File "inference.py", line 276, in main
subprocess.call(command, shell=True)
File "C:\ProgramData\Anaconda3\envs\wav2lip\lib\subprocess.py", line 287, in call
with Popen(*popenargs, kwargs) as p:
File "C:\ProgramData\Anaconda3\envs\wav2lip\lib\subprocess.py", line 729, in init
restore_signals, start_new_session)
File "C:\ProgramData\Anaconda3\envs\wav2lip\lib\subprocess.py", line 1017, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
thank you for any help for solving the issue.
Note : i set the path on the system for the ffmpeg as well which is needed for the program to run, i dont know if this is the issue.
command = 'ffmpeg -y -i {} -i {} -strict -2 -q:v 1 {}'.format(args.audio, 'temp/result.avi', args.outfile)
#Sollekram dakap
Just download and install ffmpeg from here https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-full.7z
and add it to your system environment
I am trying to call a python program with subprocess, but I get a permission error. I tried running PyCharm as an admin, but it doesn't help.
My code:
answer = subprocess.check_output("../folder python program %s %s" %(valueA, valueB), encoding = 'utf8')
The error:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/a/b/b_resolution.py", line 35, in <module>
answer = subprocess.check_output("../folder python program %s %s" %(valueA, valueB), encoding = 'utf8')
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 376, in check_output
**kwargs).stdout
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 453, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\Lib\subprocess.py", line 1155, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access Denied
Does someone know how I can fix this permission error?
Although it doesn't answer the original question, this PermissionError also arises if you (accidentally) try to run a directory, instead of a file.
For example, any of these will raise the PermissionError: [WinError 5] Access is denied:
subprocess.check_output('.')
subprocess.run('.')
where '.' represents the path to the current directory, as a minimal example.
On the other hand, if you try to run a non-existent file, you'll get a more useful FileNotFoundError: [WinError 2] The system cannot find the file specified.
Tested with python 3.10.6 on Windows and Ubuntu. On Ubuntu the examples above raise a PermissionError: [Errno 13] Permission denied.
Check the file permissions for your current user.
Right click on the file and in security you can see file permissions for users.
If you haven't permission to read file, Advanced > Select a principal then check this doc.
I fixed the problem by myself the python command comes before the path.
Like this:
answer = subprocess.check_output("python ../folder program %s %s" %(valueA, valueB), encoding = 'utf8')
But I had the problem that it says:
can't find '__main__' module in '../pydig'
Solved that aswell with writing the program name included in the path:
answer = subprocess.check_output("python ../folder/program %s %s" %(valueA, valueB), encoding = 'utf8')
close file explorer...
dumb but if you have the folder open in the explorer and you're trying to do anything to the folders/files you'll get this error
Don't run in Visual Studio 🤣. It happened to me just now.
I've been trying to get the output of the following command 'manage-bde -status' which works fine on my windows cmd commande prompt, but by using a python program.The same command doesn't work on python subprocess, so I had to launch the manage-bde.exe file.
So my code looks like this now :
import os, platform, subprocess
################This part is only helpful for resolving 32 bit/64 bits issues##########
system_root = os.environ.get('SystemRoot', 'C:\\Windows');
if (platform.architecture()[0] == '32bit' and platform.machine() == 'AMD64'):
system32='Sysnative'
else:
system32='System32'
manage_bde = os.path.join(system_root, system32, 'manage-bde.exe')
print(manage_bde)
#######################################################################################
stdout=subprocess.check_output(['start',manage_bde,'-status'])
print('Output:'+stdout)
I'm launching it from cmd commande line, with Python 3.7.0. The problem is that I get the following output :
C:\WINDOWS\Sysnative\manage-bde.exe (this is from my print(manage_bde))
Traceback (most recent call last):
File "testCLI.py", line 13, in <module>
stdout=subprocess.check_output(['start',manage_bde,'-status'])
File "d:\Profiles\user\AppData\Local\Programs\Python\Python3\lib\subprocess.py", line 376, in check_output
**kwargs).stdout
File "d:\Profiles\user\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 453, in run
with Popen(*popenargs, **kwargs) as process:
File "d:\Profiles\user\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "d:\Profiles\user\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Specified file was not found
I launch it from the D: Drive. Does anyone know what I missed ?
You probably need to run the script with administrator privileges.
If the file exists in the filesystem and you get FileNotFoundError: [WinError 2], you can test whether the the script has enough permissions to see the file with:
os.stat(manage_bde)
Yes I have enough permissions to see the file : the os.stat(manage_bde) is working.
I have changed my parameters like eryksun proposed :
stdout = subprocess.getoutput([manage_bde, '-status'])
Now I can launch the program (but only with a shell with administrator rights) and get the output, thank you !
I am trying to convert a pdf document to text document using pdftotext software.
I need to call this application inc command prompt from python script to convert the file.
I have following code:
import os
import subprocess
path = "C:\\Users\\..."
pdffname = "pdffilename.pdf"
txtfname = "txtfilename.txt"
subprocess.call(['pdftotext', '-layout',
os.path.join(path, pdffname),
os.path.join(path, txtfname)])
When I run this code, I get error
File "C:/Users/.../code-1.py", line 44, in <module>
os.path.join(path, txtfname)])
File "C:\Anaconda\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Anaconda\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Anaconda\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Can you help to call pdftotext application from python to convert pdf to text file.
I had this same error, except with Popen. I fixed it by providing the full path to pdftotext.exe in the subprocess call. Don't forget to escape your backslashes.
I do not know much about Anaconda, and I have not tested this myself, but I believe Conda may have an issue referencing scripts on Windows: fix references to scripts on windows
I am trying to execute the following code in Python
import os,subprocess
from glob import glob
path = "/Users/armed/private_SDKs"
os.chdir(path)
for file in glob("*.framework"):
command = ['class-dump' , '-H' , file , '-o' , '~', '/Users/armed/Desktop/Headers/']
subprocess.call(command)
On executing this script i get the following error :-
Traceback (most recent call last):
File "<pyshell#52>", line 3, in <module>
subprocess.call(command)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
Any help is much appreciated !
Check permissions of the class-dump command. chmod +x class-dump if it's not executable. Here are the docs on chmod.
You might also need to specify the absolute path to class-dump if it's not on your path.
Also, try:
print " ".join(command)
and see if you can run it by pasting the results into a shell.