Importing Python scripts in SQL Server stored procedure

0

I have a bunch of local Python scripts that I need to execute from a local MS SQL Server. There is no problem if I try to import standard Python libraries (for example, pandas). I get an error, when I try to import a Python file, called Simulator.py located in the C:/Users/amusaeva/PyCharmProjects/ARW/WorkforceModel folder.

EXEC sp_execute_external_script @language =N'Python',
@script=N'
import sys
sys.path.insert(0, "C:/Users/amusaeva/PyCharmProjects/ARW/WorkforceModel")
import Simulator
'

Here is the error message I get when running this SQL script:

Msg 39004, Level 16, State 20, Line 0
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.

Msg 39019, Level 16, State 2, Line 0
An external script error occurred:

Error in execution. Check the output for more information. Traceback (most recent call last):
File "", line 5, in
File "C:\ProgramData\MSSQLSERVER\Temp-PY\Appcontainer1\D4294516-2993-475D-9F61-DF7C5AF4FE69\sqlindb_0.py", line 35, in transform

import Simulator
ModuleNotFoundError: No module named 'Simulator'

SqlSatelliteCall error: Error in execution. Check the output for more information.
STDOUT message(s) from external script:
SqlSatelliteCall function failed. Please see the console output for more information.

Traceback (most recent call last):
File "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 605, in rx_sql_satellite_call
rx_native_call("SqlSatelliteCall", params)

File "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 375, in rx_native_call
ret = px_call(functionname, params)

RuntimeError: revoscalepy function failed.

I know there is definitely a Python script called Simulator.py in that WorkforceModel directory and I don't think I have any syntax mistakes because when I run the exact three lines in Python through a PS window, the module gets imported with no problem. Why does my stored procedure not see the Python script?

python
sql-server
asked on Stack Overflow Oct 26, 2020 by Aina • edited Oct 26, 2020 by marc_s

1 Answer

0

It turns out that I needed to sort out some permissions. I used this article as an inspiration: https://www.red-gate.com/simple-talk/sql/data-science-sql/sql-server-machine-learning-2019-working-with-security-changes/ and here are the exact steps I followed. The application containers are created during the SQL Server MLS setup. They are objects inside the Windows local directory, and such as all the objects in the local directory, they have a unique SID to identify the application container.

The All Application Container object has a fixed SID in the local directory, which is S-1-15-2-1 . You can use the application icacls to grant permission.

  1. Open a command prompt as an administrator.

  2. Run the following command:

    icacls C:\Users\amusaeva\PyCharmProjects\ARW\WorkforceModel /grant *S-1-15-2-1:(OI)(CI)F /t

  3. Open the SQL Server Configuration Manager, select ‘SQL Server Services’. Find ‘SQL Server Launchpad’, right-click on it and select ‘Restart’.

After this I ran my code with no issues (the only thing I needed to change was the slashes - using C:\\Users\\amusaeva\\PyCharmProjects\\ARW\\WorkforceModel).

answered on Stack Overflow Nov 16, 2020 by Aina

User contributions licensed under CC BY-SA 3.0