简体   繁体   中英

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. 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. 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?

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

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()
'

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.

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