SQL Server Machine Learning Service comes with a default Phyton runtime. For SQL 2019, it’s Phyton 3.5. You might need to run your Phyton script against a different version of Phyton runtime.
To configure another Phyton version runtime to be available to your SQL instance, do the following:
- Install Phyton 3.10 with options to install for all users
- Install required packages
- Grant required permissions to SQL Server LaunchPad service account and ALL APPLICATION PACKAGES using icacls
@ECHO OFF
ECHO installing python
"%~dp0python-3.10.11-amd64.exe" /passive TargetDir="C:\Program Files\Python310" InstallAllUsers=1 PrependPath=1 Include_test=0
ECHO installing required packages
"C:\Program Files\Python310\python.exe" -m pip install -t "C:\Program Files\Python310\Lib\site-packages" dill numpy pandas patsy python-dateutil
"C:\Program Files\Python310\python.exe" -m pip install -t "C:\Program Files\Python310\Lib\site-packages" https://aka.ms/sqlml/python3.10/windows/revoscalepy-10.0.1-py3-none-any.whl
ECHO grant required permissions: (OI)(CI)RX stands for Object Inherit, Container Inherit, Read and Execute. /inheritance:e enables inheritance if it's disabled
icacls "C:\Program Files\Python310" /grant "NT Service\MSSQLLAUNCHPAD$SQLEXP_2019":(OI)(CI)RX /T /inheritance:e
icacls "C:\Program Files\Python310" /grant *S-1-15-2-1:(OI)(CI)RX /T /inheritance:e
Please note, in the above script for grant required permission section, change MSSQLLAUNCHPAD$SQLEXP_2019 to the account running lauchpad service.
In the above script, assign permission to S-1-15-2-1 is equivelant to assign permission to ALL APPLICATION PACKAGES.
From SQL2019+, Microsoft introduced Language Extensions, which allows you to define a “Custom Runtime” alongside the default version.
-
Install the Python Version: Install your desired Python version anywhere on the server.
-
Register the External Language: Use CREATE EXTERNAL LANGUAGE to point SQL Server to a specific Python DLL/executable.
-
Run the Script: Call your script using the specific language name you created.
-- Example: Registering a custom Python 3.10 runtime
CREATE EXTERNAL LANGUAGE [Python310]
FROM (CONTENT = N'C:\Path\To\python-lang-extension-windows-release.zip', --download from Microsoft Language Extensions
FILE_NAME = 'pythonextension.dll',
ENVIRONMENT_VARIABLES = N'{
"PYTHONHOME": "C:/Program Files/Python310",
"PYTHONPATH": "C:/SQL_Python_Lib"
}' --needs to use single forwardslash or double backslash instead of single backslash
);
GO
-- Execute using your new "language" name
EXEC sp_execute_external_script
@language = N'Python310',
@script = N'import sys; print(sys.version)';
-- list all registered language
select * from sys.external_languages
select * from sys.external_language_files
Reference resources:
CREATE EXTERNAL LANGUAGE (Transact-SQL)What is SQL Server Machine Learning Services with Python and R?
Install a Python custom runtime for SQL Server
Install SQL Server 2022 Machine Learning Services (Python and R) on Windows
Install packages with Python tools on SQL Server
Change the default R or Python language runtime version