|
| 1 | +--- |
| 2 | +title: "Visual Studio Code Notebooks (Python, R)" |
| 3 | +description: Learn how to run Python and R scripts in a notebook in Visual Studio Code with SQL Server Machine Learning Services. |
| 4 | +author: VanMSFT |
| 5 | +ms.author: vanto |
| 6 | +ms.date: 12/12/2025 |
| 7 | +ms.service: sql |
| 8 | +ms.subservice: machine-learning-services |
| 9 | +ms.topic: how-to |
| 10 | +ms.custom: |
| 11 | + - sfi-image-nochange |
| 12 | +monikerRange: ">=sql-server-2017 || >=sql-server-linux-ver15" |
| 13 | +--- |
| 14 | +# Run Python and R scripts in Visual Studio Code notebooks with SQL Server Machine Learning Services |
| 15 | + |
| 16 | +[!INCLUDE [SQL Server 2017 and later](../../includes/applies-to-version/sqlserver2017.md)] |
| 17 | + |
| 18 | +Learn how to run Python and R scripts in [Visual Studio Code](https://code.visualstudio.com) notebooks with [SQL Server Machine Learning Services](../sql-server-machine-learning-services.md). Visual Studio Code is a cross-platform development tool. |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +- Download and install [Visual Studio Code](https://code.visualstudio.com)) on your workstation. Visual Studio Code is cross-platform and runs on Windows, macOS, and Linux. |
| 23 | + |
| 24 | +- Install the [Polyglot Notebooks extension](https://code.visualstudio.com/docs/languages/polyglot) for Visual Studio Code. |
| 25 | + |
| 26 | +- A server with SQL Server Machine Learning Services installed and enabled. You can use Machine Learning Services on [Windows](sql-machine-learning-services-windows-install.md), [Linux](../../linux/sql-server-linux-setup-machine-learning.md), or [Big Data Clusters](../../big-data-cluster/machine-learning-services.md). |
| 27 | + |
| 28 | +## Create a SQL notebook |
| 29 | + |
| 30 | +> [!IMPORTANT] |
| 31 | +> Machine Learning Services runs as part of SQL Server. Therefore, you need to use a SQL kernel and not a Python kernel. |
| 32 | +
|
| 33 | +You can use Machine Learning Services in Visual Studio Code with a SQL notebook. To create a new notebook, follow these steps: |
| 34 | + |
| 35 | +1. Select **File** and **New Notebook** to create a new notebook. The notebook uses the **SQL kernel** by default. |
| 36 | + |
| 37 | +1. Select **Attach To** and **Change Connection**. |
| 38 | + |
| 39 | + :::image type="content" source="media/ads-attach-to-connection.png" alt-text="Screenshot of Visual Studio Code SQL Notebook change connection."::: |
| 40 | + |
| 41 | +1. Connect to an existing or new SQL Server. You can either: |
| 42 | + |
| 43 | + 1. Choose an existing connection under **Recent Connections** or **Saved Connections**. |
| 44 | + |
| 45 | + 1. Create a new connection under **Connection Details**. Fill out the connection details to your SQL Server and database. |
| 46 | + |
| 47 | + :::image type="content" source="media/ads-connection-details.png" alt-text="Screenshot of Visual Studio Code SQL Notebook connection details."::: |
| 48 | + |
| 49 | +## Run Python or R scripts |
| 50 | + |
| 51 | +SQL Notebooks consist of code and text cells. Use code cells to run Python or R scripts through the stored procedure [sp_execute_external_scripts](../../relational-databases/system-stored-procedures/sp-execute-external-script-transact-sql.md). Use text cells to document your code in the notebook. |
| 52 | + |
| 53 | +### Run a Python script |
| 54 | + |
| 55 | +Follow these steps to run a Python script: |
| 56 | + |
| 57 | +1. Select **+ Code** to add a code cell. |
| 58 | + |
| 59 | + :::image type="content" source="media/ads-add-code.png" alt-text="Screenshot of Visual Studio Code SQL Notebooks add code block." lightbox="media/ads-add-code.png"::: |
| 60 | + |
| 61 | +1. Enter the following script in the code cell: |
| 62 | + |
| 63 | + ```sql |
| 64 | + EXECUTE sp_execute_external_script |
| 65 | + @language = N'Python', |
| 66 | + @script = N' |
| 67 | + a = 1 |
| 68 | + b = 2 |
| 69 | + c = a/b |
| 70 | + d = a*b |
| 71 | + print(c, d) |
| 72 | + '; |
| 73 | + ``` |
| 74 | + |
| 75 | +1. Select **Run cell** (the round black arrow) or press **F5** to run the single cell. |
| 76 | + |
| 77 | + :::image type="content" source="media/ads-run-python.png" alt-text="Screenshot of Visual Studio Code SQL Notebooks run Python code." lightbox="media/ads-run-python.png"::: |
| 78 | + |
| 79 | +1. The result appears under the code cell. |
| 80 | + |
| 81 | + :::image type="content" source="media/ads-run-python-output.png" alt-text="Screenshot of Visual Studio Code SQL Notebook Python code output." lightbox="media/ads-run-python-output.png"::: |
| 82 | + |
| 83 | +### Run an R script |
| 84 | + |
| 85 | +Follow these steps to run an R script: |
| 86 | + |
| 87 | +1. Select **+ Code** to add a code cell. |
| 88 | + |
| 89 | + :::image type="content" source="media/ads-add-code.png" alt-text="Screenshot of Visual Studio Code SQL Notebooks add code block." lightbox="media/ads-add-code.png"::: |
| 90 | + |
| 91 | +1. Enter the following script in the code cell: |
| 92 | + |
| 93 | + ```sql |
| 94 | + EXECUTE sp_execute_external_script |
| 95 | + @language = N'R', |
| 96 | + @script = N' |
| 97 | + a <- 1 |
| 98 | + b <- 2 |
| 99 | + c <- a/b |
| 100 | + d <- a*b |
| 101 | + print(c(c, d)) |
| 102 | + '; |
| 103 | + ``` |
| 104 | + |
| 105 | +1. Select **Run cell** (the round black arrow) or press **F5** to run the single cell. |
| 106 | + |
| 107 | + :::image type="content" source="media/ads-run-r.png" alt-text="Screenshot of Visual Studio Code SQL Notebooks run R code." lightbox="media/ads-run-r.png"::: |
| 108 | + |
| 109 | +1. The result appears under the code cell. |
| 110 | + |
| 111 | + :::image type="content" source="media/ads-run-r-output.png" alt-text="Screenshot of Visual Studio Code SQL Notebook R code output." lightbox="media/ads-run-r-output.png"::: |
| 112 | + |
| 113 | +## Related content |
| 114 | + |
| 115 | +- [SQL Notebooks in Visual Studio Code](https://devblogs.microsoft.com/dotnet/net-interactive-with-sql-net-notebooks-in-visual-studio-code/) |
| 116 | +- [Jupyter Notebooks in VS Code](https://code.visualstudio.com/docs/datascience/jupyter-notebooks) |
| 117 | +- [Polyglot Notebooks in VS Code](https://code.visualstudio.com/docs/languages/polyglot) |
| 118 | +- [Quickstart: Run simple Python scripts with SQL machine learning](../tutorials/quickstart-python-create-script.md) |
| 119 | +- [Quickstart: Run simple R scripts with SQL machine learning](../tutorials/quickstart-r-create-script.md) |
0 commit comments