Page 1 of 1

Post-processing scripts | Python on Windows = Exit(-1): Cannot run script

Posted: May 20th, 2024, 9:12 am
by anny50
python

Code: Select all

import os
import subprocess
import json

if "SAB_NZO_ID" not in os.environ:
    print("The environment variable SAB_NZO_ID is not set.")
    exit(1)
 else
   ECHO  %SAB_NZO_ID%
    
I got

Code: Select all

Exit(-1): Cannot run script
everytime.

Same Script with .bat works perectly.
I see value of SAB_NZO_ID.

Batch

Code: Select all

@echo off
setlocal

if not defined SAB_NZO_ID (
    echo Die The environment variable SAB_NZO_ID is not set.
    exit /b 1
)
If i start .py-Script by cmd:
C:\Windows\System32>C:\test\test.py

Code: Select all

The environment variable SAB_NZO_ID is not set.
This is correct, as I am not transferring any files.

Why does it work via Batch, but not via Python?

Code: Select all

import os

print("SABnzbd version:", os.environ['SAB_VERSION'])
print("Job location:", os.environ['SAB_COMPLETE_DIR'])
print("Fail msg:", os.environ['SAB_FAIL_MSG'])

# Your code

# Success code
sys.exit(0)
Same error!

What am I doing wrong?

Re: Post-processing scripts | Python on Windows = Exit(-1): Cannot run script

Posted: May 20th, 2024, 9:17 am
by safihre
Enable Debug logging in the Status window and then after it happens again click Show Logging.
In the log, it will show a Traceback why it failed to run it.

Re: Post-processing scripts | Python on Windows = Exit(-1): Cannot run script

Posted: May 20th, 2024, 9:21 am
by sander
That is not valid python code.

Make sure you can run the python code from CLI before going further

Re: Post-processing scripts | Python on Windows = Exit(-1): Cannot run script

Posted: May 20th, 2024, 9:27 am
by anny50
sander wrote: May 20th, 2024, 9:21 am That is not valid python code.

Make sure you can run the python code from CLI before going further

Code: Select all

import os

print("SABnzbd version:", os.environ['SAB_VERSION'])
print("Job location:", os.environ['SAB_COMPLETE_DIR'])
print("Fail msg:", os.environ['SAB_FAIL_MSG'])

# Your code

# Success code
sys.exit(0)
With sabnzb example i got the same error!

Logs:

Code: Select all

::DEBUG::[newsunpack:261] Failed script C:\\test.py, Traceback: 
Traceback (most recent call last):
  File "sabnzbd\newsunpack.py", line 246, in external_processing
  File "sabnzbd\misc.py", line 1232, in build_and_run_command
  File "subprocess.py", line 1026, in __init__
  File "subprocess.py", line 1538, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the specified file

Re: Post-processing scripts | Python on Windows = Exit(-1): Cannot run script

Posted: May 20th, 2024, 9:44 am
by sander
"The system cannot find the specified file" ... different error. Just solve that.

Re: Post-processing scripts | Python on Windows = Exit(-1): Cannot run script

Posted: May 20th, 2024, 9:48 am
by anny50
sander wrote: May 20th, 2024, 9:44 am "The system cannot find the specified file" ... different error. Just solve that.
Why can't it find the file if I have to explicitly specify a script folder in sabnzb and select the scripts it contains? I do not understand.

Especially as it works with the batch script.
Very confusing!

Does it not find the script or the file(s) to be processed?

Re: Post-processing scripts | Python on Windows = Exit(-1): Cannot run script

Posted: May 20th, 2024, 10:23 am
by sander
::DEBUG::[newsunpack:261] Failed script C:\\test.py, Traceback:
...
FileNotFoundError: [WinError 2] The system cannot find the specified file

So check c:\test.py does exist. If not: solve that.
If so: run "python3 c:\test.py" and see the result ...

Re: Post-processing scripts | Python on Windows = Exit(-1): Cannot run script

Posted: May 20th, 2024, 10:28 am
by anny50
sander wrote: May 20th, 2024, 10:23 am ::DEBUG::[newsunpack:261] Failed script C:\\test.py, Traceback:
...
FileNotFoundError: [WinError 2] The system cannot find the specified file

So check c:\test.py does exist. If not: solve that.
If so: run "python3 c:\test.py" and see the result ...
As I suspected, it can't be because it can't find the script.
That also makes 0 sense! Why?

Because - as I just said
- I have to explicitly specify a script folder and
- I can only select from the scripts in this folder

So it is not possible for sabnzb not to find the script, unless I manually remove the script file after I have stored it in sabnzb - but I have not done that.

But I've got it working now.
By updating from 4.2.3 to 4.3.1, the same script now shows me on sabnzb the message that the sys variable was not found.

After I have adapted your script instead of

Code: Select all

import os
it should

Code: Select all

import os
import sys
This makes the Python script work in exactly the same way as the batch script.

Re: Post-processing scripts | Python on Windows = Exit(-1): Cannot run script

Posted: May 20th, 2024, 10:36 am
by sander
Good!