获取 Python 包信息Get Python package information

适用于:Applies to: 是SQL Server 2017 (14.x)SQL Server 2017 (14.x)yesSQL Server 2017 (14.x)SQL Server 2017 (14.x) 及更高版本 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance适用于:Applies to: 是SQL Server 2017 (14.x)SQL Server 2017 (14.x)yesSQL Server 2017 (14.x)SQL Server 2017 (14.x) and later 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance

本文介绍如何获取 SQL Server 的机器学习服务上以及大数据群集上安装的 Python 包的相关信息(包括版本和安装位置)。This article describes how to get information about installed Python packages, including versions and installation locations, on Machine Learning Services on SQL Server and on Big Data Clusters. 示例 Python 脚本显示如何列出安装路径和版本等包信息。Example Python scripts show you how to list package information such as installation path and version.

本文介绍如何获取关于 SQL Server 机器学习服务上安装的 Python 包的信息(包括版本和安装位置)。This article describes how to get information about installed Python packages, including versions and installation locations, on SQL Server Machine Learning Services. 示例 Python 脚本显示如何列出安装路径和版本等包信息。Example Python scripts show you how to list package information such as installation path and version.

本文介绍如何获取关于 Azure SQL 托管实例机器学习服务上安装的 Python 包的信息(包括版本和安装位置)。This article describes how to get information about installed Python packages, including versions and installation locations, on Azure SQL Managed Instance Machine Learning Services. 示例 Python 脚本显示如何列出安装路径和版本等包信息。Example Python scripts show you how to list package information such as installation path and version.

Python 库默认位置Default Python library location

使用 SQL Server 安装机器学习时,会在实例级别为安装的每种语言创建一个包库。When you install machine learning with SQL Server, a single package library is created at the instance level for each language that you install. 实例库是注册到 SQL Server 中的安全文件夹。The instance library is a secured folder registered with SQL Server.

在 SQL Server 上的数据库内运行的所有脚本或代码都必须加载实例库中的函数。All script or code that runs in-database on SQL Server must load functions from the instance library. SQL Server 无法访问安装到其他库的包。SQL Server can't access packages installed to other libraries. 这也适用于远程客户端:在服务器计算上下文中运行的任何 Python 代码只能使用实例库中安装的包。This applies to remote clients as well: any Python code running in the server compute context can only use packages installed in the instance library. 若要保护服务器资产,只能由计算机管理员修改默认实例库。To protect server assets, the default instance library can be modified only by a computer administrator.

Python 的二进制文件的默认路径为:The default path of the binaries for Python is:

C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES

这里假设默认 SQL 实例为 MSSQLSERVER。This assumes the default SQL instance, MSSQLSERVER. 如果将 SQL Server 安装作为用户定义的命名实例,则改用给定名称。If SQL Server is installed as a user-defined named instance, the given name is used instead.

Python 的二进制文件的默认路径为:The default path of the binaries for Python is:

C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES

这里假设默认 SQL 实例为 MSSQLSERVER。This assumes the default SQL instance, MSSQLSERVER. 如果将 SQL Server 安装作为用户定义的命名实例,则改用给定名称。If SQL Server is installed as a user-defined named instance, the given name is used instead.

通过运行以下 SQL 命令来启用外部脚本:Enable external scripts by running the following SQL commands:

sp_configure 'external scripts enabled', 1;
RECONFIGURE WITH override;

重要

在 Azure SQL 托管实例上,运行 sp_configure 和 RECONFIGURE 命令会触发 SQL Server 重启,以使 RG 设置生效。On Azure SQL Managed Instance, running the sp_configure and RECONFIGURE commands triggers a SQL server restart for the RG settings to take effect. 这可能会导致几秒钟不可用。This can cause a few seconds of unavailability.

如果想验证当前实例的默认库,请运行以下 SQL 语句。Run the following SQL statement if you want to verify the default library for the current instance. 此示例返回 Python sys.path 变量中包含的文件夹列表。This example returns the list of folders included in the Python sys.path variable. 此列表包括当前目录和标准库路径。The list includes the current directory and the standard library path.

EXECUTE sp_execute_external_script
  @language =N'Python',
  @script=N'import sys; print("\n".join(sys.path))'

有关变量 sys.path 以及如何使用它为模块设置解释器搜索路径的详细信息,请参阅模块搜索路径For more information about the variable sys.path and how it's used to set the interpreter's search path for modules, see The Module Search Path.

备注

请勿尝试使用 pip 或类似方法直接在 SQL 包库中安装 Python 包。Don't try to install Python packages directly in the SQL package library using pip or similar methods. 相反,请使用 sqlmlutils 在 SQL 实例中安装包。Instead, use sqlmlutils to install packages in a SQL instance. 有关详细信息,请参阅使用 sqlmlutils 安装 Python 包For more information, see Install Python packages with sqlmlutils.

默认 Microsoft Python 包Default Microsoft Python packages

在安装过程中选择了 Python 功能时,将随 SQL Server 机器学习服务一起安装以下 Microsoft Python 包。The following Microsoft Python packages are installed with SQL Server Machine Learning Services when you select the Python feature during setup.

Packages 版本Version 说明Description
revoscalepyrevoscalepy 9.4.79.4.7 用于远程计算上下文、流式处理、并行执行进行数据导入和转换的 rx 函数、建模、可视化和分析。Used for remote compute contexts, streaming, parallel execution of rx functions for data import and transformation, modeling, visualization, and analysis.
microsoftmlmicrosoftml 9.4.79.4.7 添加 Python 机器学习算法。Adds machine learning algorithms in Python.

若要了解包含了哪个 Python 版本,请参阅 Python 和 R 版本For information on which version of Python is included, see Python and R versions.

组件升级Component upgrades

默认情况下,通过服务包和累积更新刷新 Python 包。By default, Python packages are refreshed through service packs and cumulative updates. 只能通过产品升级或将 Python 支持绑定到 Microsoft Machine Learning Server 来实现核心 Python 组件的其他包和完整版本升级。Additional packages and full version upgrades of core Python components are possible only through product upgrades or by binding Python support to Microsoft Machine Learning Server.

有关详细信息,请参阅升级 SQL Server 中的 R 和 Python 组件For more information, see Upgrade R and Python components in SQL Server.

默认开放源代码 Python 包Default open-source Python packages

如果安装过程中选择 Python 语言选项,则会安装 Anaconda 4.2 发行版 (Python 3.5)。When you select the Python language option during setup, Anaconda 4.2 distribution (over Python 3.5) is installed. 除 Python 代码库之外,标准安装还包括示例数据、单元测试和示例脚本。In addition to Python code libraries, the standard installation includes sample data, unit tests, and sample scripts.

重要

切勿手动使用 Web 上的新版本覆盖 SQL Server 安装程序安装的 Python 版本。You should never manually overwrite the version of Python installed by SQL Server Setup with newer versions on the web. Microsoft Python 包基于特定的 Anaconda 版本。Microsoft Python packages are based on specific versions of Anaconda. 修改安装可能会使其不稳定。Modifying your installation could destabilize it.

列出所有已安装的 Python 包List all installed Python packages

以下示例脚本显示了 SQL Server 实例中安装的所有 Python 包的列表。The following example script displays a list of all Python packages installed in the SQL Server instance.

EXECUTE sp_execute_external_script
  @language = N'Python',
  @script = N'
import pkg_resources
import pandas
OutputDataSet = pandas.DataFrame(sorted([(i.key, i.version) for i in pkg_resources.working_set]))'
WITH result sets((Package NVARCHAR(128), Version NVARCHAR(128)));

查找单个 Python 包Find a single Python package

如果已安装一个 Python 包并要确保它可用于特定的 SQL Server 实例,则可执行存储过程来查找该包并返回消息。If you've installed a Python package and want to make sure that it's available to a particular SQL Server instance, you can execute a stored procedure to look for the package and return messages.

例如,以下代码将查找 scikit-learn 包。For example, the following code looks for the scikit-learn package. 如果找到了该包,则代码会输出包版本。If the package is found, the code prints the package version.

EXECUTE sp_execute_external_script
  @language = N'Python',
  @script = N'
import pkg_resources
pkg_name = "scikit-learn"
try:
    version = pkg_resources.get_distribution(pkg_name).version
    print("Package " + pkg_name + " is version " + version)
except:
    print("Package " + pkg_name + " not found")
'

结果:Result:

STDOUT message(s) from external script: Package scikit-learn is version 0.20.2

查看 Python 版本View the version of Python

以下示例代码返回 SQL Server 的实例中安装的 Python 版本。The following example code returns the version of Python installed in the instance of SQL Server.

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

后续步骤Next steps