简体   繁体   English

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

[英]Importing Python scripts in SQL Server stored procedure

I have a bunch of local Python scripts that I need to execute from a local MS SQL Server.我有一堆需要从本地 MS SQL Server 执行的本地 Python 脚本。 There is no problem if I try to import standard Python libraries (for example, pandas).如果我尝试导入标准 Python 库(例如,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.当我尝试导入位于 C:/Users/amusaeva/PyCharmProjects/ARW/WorkforceModel 文件夹中的名为 Simulator.py 的 Python 文件时,出现错误。

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:这是我在运行此 SQL 脚本时收到的错误消息:

Msg 39004, Level 16, State 20, Line 0消息 39004,级别 16,状态 20,第 0 行
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.使用 HRESULT 0x80004004 执行“sp_execute_external_script”期间发生“Python”脚本错误。

Msg 39019, Level 16, State 2, Line 0消息 39019,级别 16,状态 2,第 0 行
An external script error occurred:发生外部脚本错误:

Error in execution.执行时出错。 Check the output for more information.检查输出以获取更多信息。 Traceback (most recent call last):回溯(最近一次调用最后一次):
File "", line 5, in文件“”,第 5 行,在
File "C:\\ProgramData\\MSSQLSERVER\\Temp-PY\\Appcontainer1\\D4294516-2993-475D-9F61-DF7C5AF4FE69\\sqlindb_0.py", line 35, in transform文件“C:\\ProgramData\\MSSQLSERVER\\Temp-PY\\Appcontainer1\\D4294516-2993-475D-9F61-DF7C5AF4FE69\\sqlindb_0.py”,第 35 行,转换中

import Simulator导入模拟器
ModuleNotFoundError: No module named 'Simulator' ModuleNotFoundError: 没有名为“模拟器”的模块

SqlSatelliteCall error: Error in execution. SqlSatelliteCall 错误:执行错误。 Check the output for more information.检查输出以获取更多信息。
STDOUT message(s) from external script:来自外部脚本的 STDOUT 消息:
SqlSatelliteCall function failed. SqlSatelliteCall 函数失败。 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文件“C:\\Program Files\\Microsoft SQL Server\\MSSQL15.MSSQLSERVER\\PYTHON_SERVICES\\lib\\site-packages\\revoscalepy\\computecontext\\RxInSqlServer.py”,第 605 行,在 rx_sql_satellite_call
rx_native_call("SqlSatelliteCall", params) 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文件“C:\\Program Files\\Microsoft SQL Server\\MSSQL15.MSSQLSERVER\\PYTHON_SERVICES\\lib\\site-packages\\revoscalepy\\RxSerializable.py”,第 375 行,在 rx_native_call
ret = px_call(functionname, params) ret = px_call(函数名,参数)

RuntimeError: revoscalepy function failed.运行时错误:revoscalepy 函数失败。

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.我知道在该 WorkforceModel 目录中肯定有一个名为 Simulator.py 的 Python 脚本,我认为我没有任何语法错误,因为当我通过 PS 窗口在 Python 中运行确切的三行时,模块可以毫无问题地导入。 Why does my stored procedure not see the Python script?为什么我的存储过程看不到 Python 脚本?

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.我以这篇文章为灵感: https : //www.red-gate.com/simple-talk/sql/data-science-sql/sql-server-machine-learning-2019-working-with-security-changes/这是我遵循的确切步骤。 The application containers are created during the SQL Server MLS setup.应用程序容器是在 SQL Server MLS 安装过程中创建的。 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.它们是Windows本地目录中的对象,如本地目录中的所有对象,它们都有一个唯一的SID来标识应用程序容器。

The All Application Container object has a fixed SID in the local directory, which is S-1-15-2-1 . All Application Container 对象在本地目录中有一个固定的 SID,即 S-1-15-2-1 。 You can use the application icacls to grant permission.您可以使用应用程序icacls授予权限。

  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'.打开 SQL Server 配置管理器,选择“SQL Server 服务”。 Find 'SQL Server Launchpad', right-click on it and select 'Restart'.找到“SQL Server Launchpad”,右键单击它并选择“重新启动”。

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 ).在此之后,我运行我的代码没有任何问题(我唯一需要更改的是斜杠 - 使用C:\\\\Users\\\\amusaeva\\\\PyCharmProjects\\\\ARW\\\\WorkforceModel )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM