SQL Server 2025 - Unable to run Python scripts

Peter Bishop 231 Reputation points
2025-11-25T14:50:01.7933333+00:00

I have built a SQL Server 2025 instance and, following the instructions here I have installed and configure R and Python support. R is working fine but when I try to run a simple Python script I get the errors:

EXEC sp_execute_external_script @script=N'import sys; print(sys.version)', @language=N'Python'


Msg 39012, Level 16, State 14, Line 0
Unable to communicate with the runtime for 'Python' seript for request id: 0B10DD48-FDDE-434E-933E-AB2532E2D13E. Check the requirements of 'Python' runtime.
STDERR messages from external script:
Traceback (most recent call last) :
  File "<string>", line 1, in <module>
  File "C:\Program Files\Python310\lib\site-packages\revoscalepy\_init _. py", line 127, in <module>
    from .computecontext.RxInSqlServer import RxInSqlServer
  File "C:\Program Files\Python310\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 13, in <module>
    import dill
  File "C:\Program Files\Python310\lib\site-packages\dill\_init _. py", line 26, in <module>
    from . dill import (
  File "C:\Program Files\Python310\lib\site-packages\dill\_dill.py", line 171, in <module>
    FileType = get_file_type ('rb', buffering=0)
  File "C:\Program Files\Python310\lib\site-packages\dill\_dill.py", line 164, in get_file_type
    f = open (os.devnull, *args, ** kwargs)
PermissionError: [Errno 13] Permission denied: 'nul'

Can anyone advise what might be causing this error?

Many thanks.

SQL Server Database Engine
{count} votes

Answer recommended by moderator
  1. Peter Bishop 231 Reputation points
    2025-11-29T13:50:45.3166667+00:00

    Solved it!

    Side-by-side install made no difference so I added some debugging code to "_dill.py" to report the path of "os.devnull" at the point of execution.

    On SQL2019, this returned:

    Path: C:\ProgramData\SQL2019_PROD\Temp-PY\Appcontainer1\0EE985FD-F669-434F-88D6-1D7C365240E6\nulldeviceout.txt
    

    However, on SQL2025, this returned:

    Path: nul
    

    I added similar debug code into "revoscalepy__ init __.py" and that returned the path (above) for the 2019 instance but nothing for the 2025 instance. So I expanded the exception handler (listed in my earlier "Enabling execution in AppContainer" code inclusion comment) to include:

        except Exception as e:
            if hasattr(e, 'message'):
                print(e.message)
            else:
                print(e)
    		os.devnull = oldnulldevice
    

    and this reported that the "packaging" module was missing!

    So I installed that:

    python -m pip install -t "C:\Program Files\Python310\Lib\site-packages" packaging
    

    and re-ran the icacls and Register commands (just in case) and that solved the problem!

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-25T14:50:16.7533333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The error you are encountering when trying to run Python scripts in SQL Server 2025 indicates a permission issue related to accessing the nul device. This can occur if the Launchpad service account does not have the necessary permissions to access certain system resources.

    Here are a few steps you can take to troubleshoot and potentially resolve this issue:

    1. Check Launchpad Service Account Permissions: Ensure that the account running the SQL Server Launchpad service has the necessary permissions. The account should have the following rights:
      • Log on as a service (SeServiceLogonRight)
      • Replace a process-level token (SeAssignPrimaryTokenPrivilege)
      • Bypass traverse checking (SeChangeNotifyPrivilege)
      • Adjust memory quotas for a process (SeIncreaseQuotaSizePrivilege)
    2. Verify SQLRUserGroup Permissions: Make sure that the SQLRUserGroup has the right to log on locally. If this group has had its rights modified or revoked, it may prevent the Launchpad from executing Python scripts.
    3. Antivirus Software: Check if any antivirus software is locking the folders or files that the Python script is trying to access. Antivirus software can sometimes interfere with script execution.
    4. Configuration Settings: Open SQL Server Configuration Manager and verify that the Launchpad service is running and configured correctly. Ensure that the necessary network protocols (like TCP/IP and Named Pipes) are enabled.
    5. Python Environment: Ensure that the Python environment is set up correctly and that the required packages (like dill) are installed and accessible to the SQL Server instance.

    If these steps do not resolve the issue, consider reviewing the SQL Server logs for more detailed error messages that might provide additional insights into the problem.


    References:

    1 person found this answer helpful.
    0 comments No comments

  2. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2025-11-27T19:47:16.9733333+00:00

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

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.