Sorry for not responding earlier, but I was at PASS in Seattle last week, and this week I've been suffering from jet lag.
I tried adding Python to my fresh SQL 2025 instance, following the instructions in Greg's article. (Except that I already had Python 3.10 installed.) And everything seems to work for me. I ran the below with success on my first attempt.
Really what could be wrong in your case, I don't know. But I said to myself, are we supposed to arrive at that line at all or not?
So I open the Python file and found:
def get_file_type(*args, **kwargs):
open = kwargs.pop("open", __builtin__.open)
f = open(os.devnull, *args, **kwargs)
t = type(f)
f.close()
return t
I added the silly line
x = yyy
before the call to open, and re-ran my SQL script. This time it kept looping without completing. But pressing the red button and looking in the Messages tag, I found an error message about the undefined variable yyy. Ergo: When I run my Python script from SQL Server, I arrive here. Thus, the issue could be the permission on os.devnull. Although, I am more inclined to think that args or kwargs has some funky value in your case.
This was the SQL script that worked for me:
EXEC sp_configure 'external scripts enabled', 1
RECONFIGURE
go
USE tempdb
go
-- Set up table for first example.
DROP TABLE IF EXISTS Playdata
CREATE TABLE Playdata (id int NOT NULL,
txt nvarchar(200) NULL,
CONSTRAINT pk_Playdata PRIMARY KEY (id)
)
INSERT Playdata(id, txt)
VALUES (1, N'1,2,3,4'),
(2, N'Adam, Betty, Charlie'),
(3, N'XV, LXI,VII,MCM'),
(4, N'This, is, perfect'),
(5, N'No comma in this text'),
(6, N'ABC,,DDD,, F,, XYZ')
go
-- Set up Python script for replacements.
DECLARE @python_script nvarchar(MAX) = N'
import re, pandas # Import package for regexps and pandas.
Data["txt"] = pandas.Series([ # Python/Pandas mumbo-jumbo. :-)
re.sub(r",\s*(\w)", r", \1", str) # This is where substituion happens.
for str in Data["txt"]])
'
-- Run script to get data back.
EXEC sp_execute_external_script @language = N'Python', -- Set language.
@input_data_1 = N'SELECT id, txt FROM Playdata', -- Data sent to Python.
@input_data_1_name = N'Data', -- Name of Python variable for input.
@output_data_1_name = N'Data', -- Name of Python variable for output.
@script = @python_script
EXEC sp_execute_external_script @language = N'Python',
@script = N'import sys;print(sys.version)'