I have a python script that works fine when I run it in an IDE. If I execute it from a command line, I have to be in the directory in which is resides in order for it to run properly. If I try to run it as an agent job or with an Execute Process Task in SSIS it fails.
The script inside the agent job looks like this:
py E:\Opt\AppDirectory\foo.py
SET EXITCODE = %ERRORLEVEL%
IF %EXITCODE% EQ 0 (
REM Script Ran Sucessfully
EXIT 0
)
IF %EXITCODE% EQ 1 (
REM Script Error
EXIT 1
)
When I run this, or in SSIS, I get:
Traceback (most recent call last):
File "E:\Opt\AppDirectory\foo.py", line 76, in <module>
encoder = jl.load('model.joblib')
File "C:\ProgramData\Anaconda3\lib\site-packages\joblib\numpy_pickle.py", line
590, in load
with open(filename, 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'model.joblib'
model.joblib lives in the exact same directory as foo.py. It's really weird when it says it can't find the file, but I'm staring right at it.
The job can find foo.py. Why can't it seem to find model.joblib?
When calling from SQL SERVER like you have you need to have sys.exit(0) as the last thing to do. I've ran into this so many times where the script executes fine but fails from SQL server agent.
My work around has always been to wrap XYZ in a function. In the below you will get a divide by zero to show sql server will give you the error message. If you remove that error on purpose you will see SQL server succeed.
import sys
def test():
try:
x = 1/0
if 1 > 0:
return
except BaseException as e:
print(e)
sys.exit(1)
test()
sys.exit(0)
Go ahead and check it out it should work with your:
py E:\Opt\AppDirectory\foo.py
SET EXITCODE = %ERRORLEVEL%
IF %EXITCODE% EQ 0 (
REM Script Ran Sucessfully
EXIT 0
)
IF %EXITCODE% EQ 1 (
REM Script Error
EXIT 1
)
This is not THE answer but I'm going to post what I did for now. This was for a client on a deadline so I just slapped something together. I'll post a much more in depth analysis in a few weeks.
The first thing was I needed to have an absolute path to model.joblib. That's my bad. I normally work in Jupyter Lab and forgot that was necessary.
However, that didn't fully fix the problem. The job ran but it was like it was skipping over executing the script which was weird. The final solution was to use an Execute Process Task in SSIS to run the script, then change the agent job from CmdExec to SSIS package and that made everything work right.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.