When run Python script from SQL server 2017 get error ?

  • 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\sqlindb_0.py", line 31, in transform
    for i in pip.get_installed_distributions():
    AttributeError: module 'pip' has no attribute '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\RxInSqlServer.py", line 605, in rx_sql_satellite_call
    rx_native_call("SqlSatelliteCall", params)
    File "D:\SQL Data\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 375, in rx_native_call
    ret = px_call(functionname, params)
    RuntimeError: revoscalepy function failed.

    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():
    print(i)';
    GO
  • The problem is in the error message:

    module 'pip' has no attribute 'get_installed_distributions'

    quick google of that, that module/attribute was removed in Python.  Found this on SO:

    https://stackoverflow.com/questions/49923671/are-there-any-function-replacement-for-pip-get-installed-distributions-in-pip

    Solution - use supported methods for getting what you need:

    Update
    With Python 3.8, the standard library has got a way of querying the environment for installed distributions and their metadata: importlib.metadata. For older Python versions, there's a backport importlib_metadata:

    $ pip install importlib-metadata
    It is thus advisable to use it (or the backport) instead of relying on pip's internals.

    Importing with backwards compatibility:

    import sys

    if sys.version_info >= (3, 8):
    from importlib import metadata as importlib_metadata
    else:
    import importlib_metadata
    Usage examples:

    Get names, versions and licenses (check out more available metadata keys in core metadata spec) of all installed distributions:

    dists = importlib_metadata.distributions()
    for dist in dists:
    name = dist.metadata["Name"]
    version = dist.version
    license = dist.metadata["License"]
    print(f'found distribution {name}=={version}')
    Querying single distribution by name:

    wheel = importlib_metadata.distribution('wheel')
    print(wheel.metadata["Name"], 'installed')

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply