在SQL Server存储过程中导入Python脚本

2024-10-03 06:21:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一堆本地Python脚本,需要从本地MS SQL Server执行。如果我尝试导入标准Python库(例如,pandas),则没有问题。当我尝试导入一个名为Simulator.py的Python文件时,出现了一个错误,该文件位于C:/Users/amusaeva/PyCharmProjects/ARW/WorkforceModel文件夹中

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

以下是运行此SQL脚本时收到的错误消息:

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.

我知道WorkforceModel目录中肯定有一个名为Simulator.py的Python脚本,我不认为我有任何语法错误,因为当我通过PS窗口在Python中运行三行代码时,模块导入没有问题。为什么我的存储过程没有看到Python脚本


Tags: inpyimport脚本sqlserverlinescript
1条回答
网友
1楼 · 发布于 2024-10-03 06:21:23

结果我需要整理一些权限。我用这篇文章作为灵感:https://www.red-gate.com/simple-talk/sql/data-science-sql/sql-server-machine-learning-2019-working-with-security-changes/下面是我遵循的确切步骤。 应用程序容器是在SQL Server MLS安装过程中创建的。它们是Windows本地目录中的对象,例如本地目录中的所有对象,它们具有唯一的SID来标识应用程序容器

All Application容器对象在本地目录中有一个固定的SID,即S-1-15-2-1。您可以使用应用程序icacls授予权限

  1. 以管理员身份打开命令提示符

  2. 运行以下命令:

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

  3. 打开SQL Server配置管理器,选择“SQL Server服务”。找到“SQL Server启动板”,右键单击它并选择“重新启动”

在此之后,我运行代码时没有任何问题(我唯一需要更改的是斜杠-使用C:\\Users\\amusaeva\\PyCharmProjects\\ARW\\WorkforceModel

相关问题 更多 >