I am trying to use SQL Server Agent to execute a Python file.
I am executing Python file using a Powershell script.
When I ran the SQL Server Agent job, error message says:
Message
Executed as user: NT Service\SQLAgent$SQL2022. Start-Process : This command cannot be run
due to the error: "Access is denied At F:\xx.ps1:5 char:1 + Start-Process
C:\Users\jdoe\AppData\Local\Programs\Python\Python312\p ... +
I am thinking this happens because I originally installed Python using my own local access (username: jdoe).
Today, I asked our IT to install as Admin.
Currently, python is installed in two locations:
(When I installed myself originally):
C:\Users\jdoe\AppData\Local\Programs\Python\Python312\python.exe
(When IT just installed today):
C:\Program Files\Python313\python.exe
What do I need to do run Python code SQL Server Agent?
Where do I have to configure?
1 Answer 1
Use the system-wide Python installation
Since IT installed Python under:
C:\Program Files\Python313\python.exe
that’s perfect — it’s in a location accessible to all users and system accounts.
Update your PowerShell script to call the correct Python
In your .ps1 script, make sure it points explicitly to the system-wide Python, e.g.
Start-Process -FilePath "C:\Program Files\Python313\python.exe" -ArgumentList "C:\Path\To\your_script.py"
Make sure the SQL Agent service account has access to your Python script and any data files
Ensure the folder containing your_script.py grants 'Read & Execute' permissions to the NT SERVICE\SQLAgent$SQL2022 account (or whatever account the SQL Server Agent runs as)
If your Python script reads/writes files elsewhere, check those folders as well.
Add system-wide Python to the PATH
If you want to call python.exe without a full path then add
C:\Program Files\Python313\
to the system PATH variable. And then restart the SQL Server Agent service.
Restart SQL Server agent
Whenever you make permission changes or environment changes (e.g., PATH), restart the SQL Server Agent service so it picks them up.
Hope it helps.
Explore related questions
See similar questions with these tags.