In this post, we’ll dive into how you can run Python code in SQL Server 2022. This process has changed significantly from previous versions, making it much easier and more in line with how developers typically use Python.
What Changed?
Before SQL Server 2022, running Python inside SQL required working with a Microsoft-managed version of Python packages. This often-caused headaches because:
- Microsoft’s Python builds lagged behind official Python releases.
- Installing new packages was difficult since they weren’t always available in Microsoft’s repository.
- The overall implementation felt clunky and limited compared to using Python natively.
The Big Improvement in SQL 2022
With native Python support, SQL Server 2022 now lets you use your locally installed Python environment. This means:
- You can install and manage Python packages as you normally would.
- SQL simply connects to your default Python installation instead of running a separate Microsoft-managed version.
- Code execution happens directly in Python, not inside SQL Server.
When Should You Use Python Inside SQL?
Most developers will still prefer to run Python scripts separately, outside of SQL Server. However, using Python within T-SQL can be useful for:
- ETL (Extract, Transform, Load) tasks, like data cleaning and transformation.
- Situations where you want to apply Python-based processing directly within SQL queries.
That said, there’s no shortage of creative ways to integrate Python with SQL!
For a step-by-step guide on using Python in older SQL Server versions, check out this link:
SQL Server Machine Learning Using Python – Step by Step
Installing Python (3.10)
Important: Use Python 3.10 for SQL Server 2022
For now, only Python 3.10 works reliably with SQL Server 2022. If you try using newer versions (like Python 3.12+), you may run into issues—notably, revoscalepy packages will fail to install due to changes in pip.
Why Start with Python Installation First?
I recommend installing Python before setting up SQL integration because:
- It’s an easier first step.
- Some later steps require knowing your Python installation path.
Official Microsoft Support
Microsoft has officially tested and documented Python 3.10 as the compatible version for SQL 2022, so sticking with it will save you troubleshooting headaches.
Download & Install Python 3.10
You can download the Python 3.10 installer here:
👉 Python 3.10 Download
During installation, be sure to:
✅ Choose Custom Installation
✅ Check “Use Admin”
✅ Check “Add to PATH” (very important!) Following these steps will ensure your Python setup works smoothly with SQL Server 2022.

Install Python in a Non-Privileged Folder
According to Microsoft’s official documentation, it’s best to install Python in a non-privileged folder (i.e., outside system-protected locations). This makes granting permissions later much easier.
Steps to Follow:
During installation,
- Change the installation path to a simple location on your C:\ drive (e.g., C:\Python310).
Remember this path—you’ll need it later! - Check “Install Python for all users”—this simplifies permissions and ensures smooth integration with SQL Server.
Following these steps will prevent unnecessary permission issues and make your setup hassle-free.

Installing machine learning services for SQL Server 2022
First, download the SQL Server 2022 ISO and start the installation process. If you need help, there are plenty of tutorials and guides available online, including on this blog—so we won’t be covering the full installation steps here. Feel free to refer to those as needed.
Enabling Machine Learning Services
During the installation, when you reach the Feature Selection screen, make sure to check the box for Machine Learning Services (as shown below).

Once the Machine Learning Services feature is installed, you’ll need to run the code in the next section to download the required package.
Setup and configuration
Once Python and Machine Learning Services are installed, the rest of the process is straightforward.
Simply follow the steps outlined in the official Microsoft documentation:
👉 SQL Server Machine Learning Services Installation Guide
These steps have been tested and confirmed to work.
💡 Why not copy-paste the instructions here?
- Your installation path may differ from the one in the documentation.
- Microsoft may update the steps in the future, and I want to ensure this guide stays relevant.
Make sure the run them in an elevated command prompt window
You need a lot more memory when running SQL and Python on the same machine.
This is an example of what the results will look like when the python code is executed,

Errors you may face if you installed Python 3.12 or above
Error | Solution |
AttributeError: module ‘pkgutil’ has no attribute ‘ImpImporter’. Did you mean: ‘zipimporter’? | pkgUtil is no longer supported so you end up getting stuck here. Hence we use 3.10 version of Python which has the module and therefore is able to install the packages from the link mentioned in the MS documentation. |
ModuleNotFoundError: No module named ‘distutils.util’ | In order to rectify this, you need to check if the path specified in the command earlier is correct. If not rectify and rerun the command else run the below command in an elevated command prompt, also make sure your version of Pip is updated to the latest version. (Pip is the Play store for Python , sort off 😊) pip install setuptools |