【问题标题】:How to connect to Snowflake using python snowflake connector from within Databricks in Python 3?如何在 Python 3 的 Databricks 中使用 python 雪花连接器连接到雪花?
【发布时间】:2019-06-18 08:10:45
【问题描述】:

当我尝试将 snowflake-sqlalchemy 库附加到 Databricks 中的 Python 3 集群时,它会破坏我的 Python 构建,并且在我安装后续库时出现以下错误:

AttributeError: cffi library '_openssl' has no function, constant or 名为“Cryptography_HAS_ED25519”的全局变量

我曾尝试将最新版本的 Cryptography 库单独附加到集群,但这给了我同样的问题。我认为可能与以下链接有关:

connecting-to-snowflake-from-azure-databricks-notebook-message-openssl-has-no-function-constant-or-global-variable-named-cryptography

https://github.com/snowflakedb/snowflake-connector-python/issues/32

在第二个链接中提到了一种解决方法:

The workaround is:
Uninstall cryptography by running pip uninstall cryptography
Delete the directory .../site-packages/cryptography/ manually
Reinstall snowflake-connector-python

Looks like the directory structure of cryptography changed since 1.7.2.*

有什么方法可以卸载 Databricks 中预装的 cryptography 1.5 python 库,以便我可以使用新的目录结构重新安装最新版本的 cryptography (2.5)?

【问题讨论】:

    标签: python databricks snowflake-cloud-data-platform python-cryptography


    【解决方案1】:

    过时的库:

    %sh sudo apt-get install python3-pip -y
    

    接着是:

    %sh pip3 install --upgrade snowflake-connector-python
    

    更多详情请参阅https://datathirst.net/blog/2019/1/11/databricks-amp-snowflake-python-errors

    【讨论】:

    • 此解决方案可能适用于 snowflake-connector-python,但不适用于 snowflake-sqlalchemy。我为我的问题找到了不同的解决方案,并在下面发布了答案。
    【解决方案2】:

    我找到了问题的答案。

    这个问题是由于 Databricks 中的 openssl 版本太旧导致 snowflake-sqlalchemy 无法使用它。

    解决方法如下:

    1. 升级画中画

      %sh /databricks/python/bin/pip install --upgrade pip

    2. 卸载 pyopenssl

      %sh /databricks/python/bin/pip 卸载 pyopenssl -y

    3. 安装pyopenssl

      %sh /databricks/python/bin/pip install --upgrade pyopenssl

    4. 安装雪花-sqlalchemy

      %sh /databricks/python/bin/pip install --upgrade snowflake-sqlalchemy

    这个问题的答案很有帮助: Python AttributeError: 'module' object has no attribute 'SSL_ST_INIT'

    我使用以下代码创建了一个初始化文件:

    dbutils.fs.mkdirs("dbfs:/databricks/init/")
    
    dbutils.fs.put("dbfs:/databricks/init/sf-initiation.sh" ,"""
    #!/bin/bash
    /databricks/python/bin/pip install --upgrade pip
    /databricks/python/bin/pip uninstall pyopenssl -y
    /databricks/python/bin/pip install --upgrade pyopenssl
    /databricks/python/bin/pip install --upgrade snowflake-sqlalchemy
    """, True)
    

    文件中的最后一条命令更新所有过时的包,如下所示: Upgrading all packages with pip

    【讨论】:

      最近更新 更多