【问题标题】:sp_execute_external_script can't find modules installed by setuptoolssp_execute_external_script 找不到 setuptools 安装的模块
【发布时间】:2020-04-24 06:11:51
【问题描述】:

我正在积极开发一个 Python 模块,我想在本地安装的 SQL Server 2017 中进行部署,因此我将模块部署在 c:\Program Files\Microsoft SQL Server\<Instance Name>\PYTHON_SERVICES\Lib\site-packages使用 setuptools,如下所示:

"c:\Program Files\Microsoft SQL Server\<Instance_Name>\PYTHON_SERVICES\python" setup.py develop

这会在我的项目根目录中生成一个.egg-info 目录,并在上面提到的site-packages 目录中生成一个.egg-link 文件。 .egg-link 文件正确指向我的项目根目录中的.egg-info 目录,因此看起来setuptools 工作正常。

这是我的setup.py供参考:

from setuptools import setup

setup(
    setup_requires=['pbr'],
    pbr=True,
)

这是对应的setup.cfg文件:

[metadata]
name = <module_name>
description = <Module Description>
description-file = README.md
description-content-type = text/markdown

[files]
package_root = py/src

由于我只是想让管道工作,我在&lt;project_root&gt;/py/src 中有一个名为uploader.py 的python 脚本:

#uploader.py
class Upload:
    pass

有了这个部署,我希望简单地将我刚刚通过.egg-link 发布的模块import 转换为sp_execute_external_script 调用,如下所示:

execute sp_execute_external_script @language= N'Python', @script= N'from <module_name>.uploader import Upload';

但是,从 SSMS 执行此存储过程会产生以下错误消息:

Msg 39004, Level 16, State 20, Line 10
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 10
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 "C:\SQL-MSSQLSERVER-ExtensibilityData-PY\MSSQLSERVER01\C08BB9A7-66B5-4B5E-AAFC-B0248EE64199\sqlindb.py", line 27, in transform
    from <module_name>.uploader import Upload
ImportError: No module named '<module_name>'

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 "C:\Program Files\Microsoft SQL Server\<Instance_Name>\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 587, in rx_sql_satellite_call
    rx_native_call("SqlSatelliteCall", params)
  File "C:\Program Files\Microsoft SQL Server\<Instance_Name>\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 358, in rx_native_call
    ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.

我显然已经从错误消息中删除了module_nameInstance_Name

我尝试使用install 命令而不是develop 只是为了确保.egg-link 文件没有问题。 installsite-packages 中安装了.egg-info 文件,但我得到了同样的错误。

我也尝试从组合中删除 pbr,但得到了同样的错误。

最后,我尝试按照How can I use an external python module with SQL 2017 sp_execute_external_script? 的建议将我的&lt;project_root&gt; 添加到sys.path,但这也没有帮助。

所以在这一点上,我不知道我可能做错了什么。

python 版本是3.5.2,我认为项目中不需要__init__.py 才能使其成为模块。在py/src 中插入空白__init__.py 也无济于事。

我的pip 版本是19.3.1setuptools 版本是44.0.0pbr 版本是5.4.4 并且我已经确认所有模块都安装在上面提到的site-packages 目录中。

【问题讨论】:

    标签: python sql-server setuptools egg


    【解决方案1】:

    根据我的大量实验,sp_execute_external_script 似乎没有遵循符号链接(即通过.egg-link 文件)。因此,无论您使用setuptoolspippbr 还是其他任何方式,开发模式安装都将不起作用。

    我什至尝试将&lt;package_name&gt; 文件夹符号链接为操作系统符号链接。因为我在 Windows 上,所以我在命令提示符上使用 mklink /D 命令在 site-packages 内符号链接 /py/src/&lt;package_name&gt;。虽然命令正确执行,并且我可以在文件资源管理器中看到符号链接文件夹,但 sp_execute_external_script 无法检测到包。这告诉我sp_execute_external_script 代码中可能有一些东西可以避免遍历符号链接。

    不知道有没有办法让它遍历符号链接。

    唯一可行的解​​决方案是在它自己的目录下开发一个包的代码,所以,在我的例子中是/py/src/&lt;package_name&gt;。然后,在运行exec sp_execute_external_script @language=N'python', @script=N'...' 之前,将&lt;package_name&gt; 文件夹复制到site-packages 目录。

    这在某种程度上等同于setup.py install,但绕过了中间文件和目录的创建。所以我将坚持使用这种简单但可恶的方法。

    我希望有更多知识的人能提供更好的方法来解决这个问题。

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 2010-12-31
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多