Error when execute python script from sql server 2017 query ?

ahmed salah 3,216 Reputation points

when run python script from sql server 2017 I get error
Msg 39004, Level 16, State 20, Line 0
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 0
An external script error occurred:

Error in execution.  Check the output for more information.
Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "D:\ProgramData\MSSQLSERVER\Temp-PY\Appcontainer1\3E1B378D-D357-4C9C-991B-F07D4A51A5AC\", line 31, in transform
    for i in pip.get_installed_distributions():
AttributeError: module 'pip' has no attribute 'get_installed_distributions'

so how to solve issue please ?

when install sql server 2017 i add python option

What I have tried:

EXECUTE sp_execute_external_script
@language =N'Python',
@script=N'import pip
for i in pip.get_installed_distributions():

SqlSatelliteCall error: Error in execution.  Check the output for more information.
STDOUT message(s) from external script:
SqlSatelliteCall function failed. Please see the console output for more information.
Traceback (most recent call last):
  File "D:\SQL Data\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\", line 605, in rx_sql_satellite_call
    rx_native_call("SqlSatelliteCall", params)
  File "D:\SQL Data\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\", line 375, in rx_native_call
    ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,320 questions
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,597 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Seeya Xi-MSFT 16,461 Reputation points

    Hi @ahmed salah ,

    Welcome to Microsoft Q&A!
    You cannot use get_installed_distributions() directly after pip upgrade to 10.0. You can try the method in the link mentioned by NaomiNNN.
    You can use from pip._internal.utils.misc import get_installed_distributions. This should work, but this method is strongly not recommended.

    AttributeError: module 'pip' has no attribute 'get_installed_distributions'

    Here is a document that explains the reason for this error. See this pip document.
    Here is a similar thread please have a look:
    After reading it, you will find setuptools (specifically pkg_resources) may be a good choice for you.

    Best regards,

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments