SQL Server Python tutorials

THIS TOPIC APPLIES TO: yesSQL Server (Windows only)noAzure SQL DatabasenoAzure SQL Data WarehousenoParallel Data Warehouse

This article provides a list of tutorials and samples that demonstrate the use of Python with SQL Server 2017. Through these samples and demos, you will learn:

  • How to run Python from T-SQL
  • What are remote and local compute contexts, and how you can execute Python code using the SQL Server computer
  • How to wrap Python code in a stored procedure
  • Optimizing Python code for a SQL production environment
  • Real-world scenarios for embedding machine learning in applications

For information about requirements and setup, see Prerequisites.

Python tutorials

  • Running Python in T-SQL

    Learn the basics of how to call Python in T-SQL, using the extensibility mechanism pioneered in SQL Server 2016.

  • Create a machine learning model in Python using revoscalepy

    This lesson demonstrates how you can run code from a remote Python terminal, using SQL Server compute context. You should be somewhat familiar with Python tools and environments. Sample code is provided that creates a model using rxLinMod, from the new revoscalepy library.

  • In-Database Python analytics for SQL developers

    This end-to-end walkthrough demonstrates the process of building a complete Python solution using T-SQL stored procedures. All Python code is included.

Python samples

These samples and demos provided by the SQL Server development team highlight ways that you can use embedded analytics in real-world applications.

Prerequisites

To use these tutorials, you must have SQL Server 2017, and you must explicitly install and then enable the feature, Machine Learning Services (In-Database).

SQL Server 2017 supports both the R and Python languages, but neither is installed or enabled by default. Running Python requires that the extensibility framework be enabled, and that you select Python as the language to install.

Post-installation configuration tips

After running SQL Server setup, you might need to perform some additional steps to ensure that Python and SQL Server are communicating:

  • Enable the external script execution feature by running sp_configure 'external scripts enabled', 1.
  • Restart the server.
  • Open the Services panel to check whether Launchpad has started.
  • Ensure that the service that calls the external runtime has necessary permissions. For more information, see Enable implied authentication.
  • Open a port on the firewall for SQL Server, and enable required network protocols.
  • Ensure that your SQL login or Windows user account has necessary permissions to connect to the server, to read data, and to create any database objects required by the sample.

See this article for some common issues: Troubleshooting Machine Learning Services

Resource management

You can install both R and Python on the same computer, but running both can require substantial resources. If you get "out of memory" errors, or if running machine learning jobs is the principal intended use of the server, you can reduce the amount of memory that is allocated to the database engine. For more information, see Managing and monitoring Python in SQL Server.

See also

R tutorials for SQL Server