4

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?

2
  • dba.stackexchange.com/questions/188907/… Commented Dec 31, 2018 at 1:21
  • @MitchWheat, as far as I can tell this will only let me import a public module which I've installed to the server. I'm not sure if this method will allow me to import a private module I've written myself? Commented Jan 3, 2019 at 1:24

1 Answer 1

3

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()
'
Sign up to request clarification or add additional context in comments.

1 Comment

Great stuff, wish you were there when I needed you months ago! Joking aside, thank you. Hopefully this helps somebody else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.