Get R and Python package information

APPLIES TO: yesSQL Server (Windows only) noAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

Sometimes when you are working with multiple environments or installations of R or Python, you need to verify that the code you are running is using the expected environment for Python or the correct workspace for R. For example, if you have upgraded the machine learning components through binding, the path to the R library might be in a different folder than the default. Also, if you install R Client or an instance of the Standalone server, you might have multiple R libraries on your computer.

R and Python script examples in this article show you how to get the path and version of packages used by SQL Server.

Get the R library location

For any version of SQL Server, run the following statement to verify the default R package library for the current instance:

EXECUTE sp_execute_external_script  
  @language = N'R',
  @script = N'OutputDataSet <- data.frame(.libPaths());'

Optionally, you can use rxSqlLibPaths in newer versions of RevoScaleR in SQL Server 2017 Machine Learning Services or R Services upgraded R to at least RevoScaleR 9.0.1. This stored procedure returns the path of the instance library and the version of RevoScaleR used by SQL Server:

EXECUTE sp_execute_external_script
  @language =N'R',
  sql_r_path <- rxSqlLibPaths("local")
  version_info <-packageVersion("RevoScaleR")


The rxSqlLibPaths function can be executed only on the local computer. The function cannot return library paths for remote connections.


STDOUT message(s) from external script: 
[1] "C:/Program Files/Microsoft SQL Server/MSSQL14.MSSQLSERVER1000/R_SERVICES/library"
[1] '9.3.0'

Get the Python library location

For Python in SQL Server 2017, run the following statement to verify the default library for the current instance. 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))'


C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\lib
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\Sphinx-1.5.4-py3.5.egg
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\win32
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\win32\lib
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\Pythonwin
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\setuptools-27.2.0-py3.5.egg

For more information about the variable sys.path and how it is used to set the interpreter's search path for modules, see the Python documentation

List all packages

There are multiple ways that you can get a complete list of the packages currently installed. One advantage of running package list commands from sp_execute_external_script is that you are guaranteed to get packages installed in the instance library.


The following example uses the R function installed.packages() in a Transact-SQL stored procedure to get a matrix of packages that have been installed in the R_SERVICES library for the current instance. This script returns package name and version fields in the DESCRIPTION file, only the name is returned.

EXECUTE sp_execute_external_script
  @script = N'str(OutputDataSet);
  packagematrix <- installed.packages();
  Name <- packagematrix[,1];
  Version <- packagematrix[,3];
  OutputDataSet <- data.frame(Name, Version);',
  @input_data_1 = N''
WITH RESULT SETS ((PackageName nvarchar(250), PackageVersion nvarchar(max) ))

For more information about the optional and default fields for the R package DESCRIPTION field, see


The pip module is installed by default, and supports many operations for listing installed packages, in addition to those supported by standard Python. You can run pip from a Python command prompt, of course, but you can also call some pip functions from sp_execute_external_script.

EXECUTE sp_execute_external_script 
  @language = N'Python', 
  @script = N'
  import pip
  import pandas as pd
  installed_packages = pip.get_installed_distributions()
  installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages])
  df = pd.DataFrame(installed_packages_list)
  OutputDataSet = df'
WITH RESULT SETS (( PackageVersion nvarchar (150) ))

When running pip from the command line, there are many other useful functions: pip list gets all packages that are installed, whereas pip freeze lists the packages installed by pip, and doesn't list packages that pip itself depends on. You can also use pip freeze to generate a dependency file.

Find a single package

If you have installed a package and want to make sure that it is available to a particular SQL Server instance, you can execute the following stored procedure call to load the package and return only messages.


This example looks for and loads the RevoScaleR library, if available.

EXECUTE sp_execute_external_script  
  @language =N'R',
  • If the package is found, a message is returned: "Commands completed successfully."

  • If the package cannot be located or loaded, you get an error containing the text: "there is no package called 'MissingPackageName'"


The equivalent check for Python can be performed from the Python shell, using conda or pip commands. Alternatively, run this statement in a stored procedure:

EXECUTE sp_execute_external_script
  @language = N'Python',
  @script = N'
  import pip
  import pkg_resources
  pckg_name = "revoscalepy"
  pckgs = pandas.DataFrame([(i.key) for i in pip.get_installed_distributions()], columns = ["key"])
  installed_pckg = pckgs.query(''key == @pckg_name'')
  print("Package", pckg_name, "is", "not" if installed_pckg.empty else "", "installed")'

Get package version

You can get R and Python package version information using Management Studio.

R package version

This statement returns the RevoScaleR package version and the base R version.

EXECUTE sp_execute_external_script
  @language = N'R',
  @script = N'

Python package version

This statement returns the revoscalepy package version and the version of Python.

EXECUTE sp_execute_external_script
  @language = N'Python',
  @script = N'
import revoscalepy
import sys

Next steps