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

Come up with a useful post-processing script? Share it here!
Post Reply
anny50
Newbie
Newbie
Posts: 11
Joined: July 12th, 2020, 9:07 am

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

Post 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?
Last edited by anny50 on May 20th, 2024, 9:42 am, edited 2 times in total.
User avatar
safihre
Administrator
Administrator
Posts: 5521
Joined: April 30th, 2015, 7:35 am
Contact:

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

Post 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.
If you like our support, check our special newsserver deal or donate at: https://sabnzbd.org/donate
User avatar
sander
Release Testers
Release Testers
Posts: 9061
Joined: January 22nd, 2008, 2:22 pm

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

Post by sander »

That is not valid python code.

Make sure you can run the python code from CLI before going further
anny50
Newbie
Newbie
Posts: 11
Joined: July 12th, 2020, 9:07 am

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

Post 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
User avatar
sander
Release Testers
Release Testers
Posts: 9061
Joined: January 22nd, 2008, 2:22 pm

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

Post by sander »

"The system cannot find the specified file" ... different error. Just solve that.
anny50
Newbie
Newbie
Posts: 11
Joined: July 12th, 2020, 9:07 am

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

Post 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?
User avatar
sander
Release Testers
Release Testers
Posts: 9061
Joined: January 22nd, 2008, 2:22 pm

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

Post 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 ...
anny50
Newbie
Newbie
Posts: 11
Joined: July 12th, 2020, 9:07 am

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

Post 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.
User avatar
sander
Release Testers
Release Testers
Posts: 9061
Joined: January 22nd, 2008, 2:22 pm

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

Post by sander »

Good!
Post Reply