简体   繁体   English

如何在 SQL 2017 sp_execute_external_script 中使用外部 python 模块?

[英]How can I use an external python module with SQL 2017 sp_execute_external_script?

I'm testing out the SQL 2017 machine learning services that allow you to run python scripts in a stored procedure.我正在测试允许您在存储过程中运行 python 脚本的 SQL 2017 机器学习服务。 I see plenty of examples of how to run a python script when the script is defined in the stored procedure itself, but I'd like to know how to import my own python modules.当脚本在存储过程本身中定义时,我看到了很多关于如何运行 python 脚本的示例,但我想知道如何导入我自己的 python 模块。 Something like this:像这样的东西:

EXEC sp_execute_external_script
@language = N'Python',
@script = N'
from test import sample
x = sample.SomeClass()
x.SomeFunction()
'

Is this possible?这可能吗? Is there another method for my to run my own python scripts in SQL?是否有另一种方法可以让我在 SQL 中运行我自己的 Python 脚本?

Yes you may do this by extending the sys.path environment variables by the adding the location of test module in your code like this是的,您可以通过在代码中添加test模块的位置来扩展 sys.path 环境变量来做到这一点

EXEC sp_execute_external_script
@language = N'Python',
@script = N'
import sys
sys.path += ['D:\\path_to_your_test_module']
from test import sample as sa
x = sa.SomeClass()
x.SomeFunction()
'

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

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