Python subprocess calling - python

I have a problem with running files through python.
This is my code :
def report1(self):
str="/Users/Apple/Desktop/Report1.exe"
subprocess.call(str)
This is the error i am getting :
File "./DBMS.py", line 427, in <module>
Main().run();
File "./DBMS.py", line 415, in run
self.report1()
File "./DBMS.py", line 383, in report1
subprocess.call(str)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
PS: i tried changing permission rights on folders and i tried using subprocress.Popen .
i also tried adding shell=True. i don't understand why its still not working .
any help is really appreciated.
have to submit in 24 hours :(

For all its merits, subprocess doesn't make it abundantly clear when an error has occurred while trying to execute a command.
If the deepest frame in your traceback (the one right before the actual exception) is raise child_exception from subprocess.py, that means there was some issue inclusively between the fork(2) and exec*(2) calls -- in other words, that an error occurred while trying to run the command you requested.
The actual exception you pasted was OSError: [Errno 13] Permission denied. An errno of 13 corresponds to EACCES:
>>> import errno; print errno.errorcode[13]
EACCES
If you've never used fork(2) or exec(2) things will be pretty inscrutable, because subprocess has dropped the real traceback. However, I can tell you that this OSError almost certainly came from the exec* call. It turns out execve raises this under the following conditions:
[EACCES] Search permission is denied for a component of the
path prefix.
[EACCES] The new process file is not an ordinary file.
[EACCES] The new process file mode denies execute permission.
[EACCES] The new process file is on a filesystem mounted with
execution disabled (MNT_NOEXEC in <sys/mount.h>).
(Courtesy of Apple)
If I had to guess, you encountered this this exception because the command you're trying to run isn't marked executable (with something like chmod u+x).
Now, it's unlikely that your .exe file will run on your Mac after solving this, but at least it'll be a different error!

Try running your program as sudo:
sudo python mycode.py

Related

PermissionError: [WinError 5] Access denied

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.

Getting 'OSError: [Errno 2] No such file or directory' on popen when trying to run mypy

Hello stack overflow users,
I'm having a great deal of difficulty trying to get mypy to work correctly with Popen. Essentially I'm writing a test suite that utilizes mypy but cannot seem to get valid output on my build machine.
The line in question is the following:
p = subprocess.Popen(['mypy'] + extra_args + python_files, env=my_env, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIP)
extra_args specifies the cache-dir and traceback, and python_files are the files in question to be tested. I'm fairly confident that neither of these arguments are the issue, as I've played around with these and made no progress.
This file is in a git repo, and I've cloned it and put it on a build box. Locally it's not a problem -- I get valid, mypy output indicating errors. However, on my build machine I get the OSError: [Errno 2] No such file or directory error. The full error message is as follows:
Traceback (most recent call last):
File "mypath/run_mypy.py", line 83, in <module>
main()
File "mypath/run_mypy.py", line 70, in main
p = subprocess.Popen(['mypy'] + extra_args + python_files, env=my_env, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
myotherpath/build-stack.mk:162: recipe for target 'mypy' failed
I've tried adding shell=True but this resulted in all mypy errors begin suppressed, plus is considered bad style/security concern.
My guess/thought is that maybe mypy somehow isn't in the path that popen uses without the shell but I'm really not sure.
Okay, just figured out my problem and it was fairly obvious... Whoops!
This fixed it: python3 -m pip install -U mypy==0.530
Turns out I didn't have mypy installed on the box. The error message led me astray because I thought it was referring to one of the arguments I'd passed, not the actual command. Hopefully this is helpful to someone in the future.

Error running python script in terminal: OSError: [Errno 2] No such file or directory

I'm trying to use a piece of software called "bundler_sfm" which is executed using a python script.
The software I'm trying to use is available here, the script is in the utils directory if you want to have a look.
When trying to run it I get the following python error:
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
The code that leads to this error is as follows:
# Extract SIFT data
if verbose:
with open(pgm_filename, 'rb') as fp_in:
with open(key_filename, 'wb') as fp_out:
subprocess.call(BIN_SIFT, stdin=fp_in, stdout=fp_out)
I've looked at various other answers with similar errors but am still at a loss on how to fix this problem.
I'm trying to run this in the terminal on elementary OS.
Any assistance would be greatly appreciated.
Already worked it out in the comments, but just for an answer:
Worked out the location where the utility thinks the sift binary is located by printing out BIN_SIFT before calling subprocess.call() method.
Realized this path was incorrect
As a hackish work-around, hard code the correct path to line 55 of bundler.py as a string inside of a list:
BIN_SIFT = ["/real/path/to/sift"]

Python running bash script and generate OSError

I have a bash script which helps establish a local SimpleHTTPServer.
python -m SimpleHTTPServer 8080
I have put this inside my project folder. While I am running the program by using:
subprocess.call('./setup.sh')
an error message comes out:
Traceback (most recent call last):
File "test.py", line 2, in <module>
subprocess.call('./setup.sh')
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
I retried this in terminal
localhost:Desktop XXXX$ sh setup.sh
Serving HTTP on 0.0.0.0 port 8080 ...
It is working fine.
I remember there are a few times where the terminal has popped up a window ask me about the permission for python about something related to firewall and I allowed it. Can you help me?
Run it exactly as you would on the shell, i.e., as sh ./setup.sh:
subprocess.call('sh ./setup.sh', shell=True)
That should do the trick. Most likely, your setup.sh is not set to executable or is missing the first #! line that marks its interpreter.
EDIT:
Make sure to set shell=True to execute it via the shell, if you pass it as a single string, or separate the parameters into a list, as you might with execve:
subprocess.call(['sh', './setup.sh'])
Give subprocess.Popen() a try, with cwd param:
subprocess.Popen(['sh', './setup.sh'], cwd='/dir/contains/setup.sh/')

Python script works fine on Linux, on Windows, causes WindowsError: [Error 5] Access is denied

I have a simple python script that works fine on Linux, I moved it to a Windows machine and when I attempt to run it, I get the following exception message:
Traceback (most recent call last):
File "C:\path\to\my\script.py", line 57, in <module>
retcode = subprocess.call(command)
File "C:\Python27\lib\subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
Here is the snippet of code that throws the exception:
print 'command is:',command
retcode = subprocess.call(command)
The console out put is as follows:
command is: ['c:\python27', 'C:\path\to\script.py', '--mode=2', '--check-temp=false', '--all-seasons=true', '--added=1', '--max-temp=2000', '--source=2', '--loc=XYZ']
Unhandled exception while debugging...
Anyone knows how to fix this?
I am running python v2.7.3 on Windows XP Professional
According to the documentation, the first item in the argument sequence (in this case, the first element of command) is interpreted as the program to execute.
Looking at the first element of command, it would appear that you're trying to execute a directory. Windows (somewhat non-intuitively) returns an access denied error whenever you try to read from a directory as if it were a file, and the same thing happens if you try to execute one.
Instead of c:\python27 you probably want c:\python27\bin\python.exe or something similar. At any rate, you need to be pointing at the executable, not at the directory. I'm not sure why this works for you on Linux.
Your program doesn't have access to the file... check permissions on the file you're trying to access, then go from there... (ie, either elevate the Python interpreter's permissions, or reduce access required to said resource) - either way - tread carefully.

Categories

Resources