snowflake-connector-python icon indicating copy to clipboard operation
snowflake-connector-python copied to clipboard

SNOW-507597: Not supported error on Apple M1

Open richiejthomas opened this issue 5 years ago • 33 comments

Please answer these questions before submitting your issue. Thanks!

  1. What version of Python are you using?

    Python 3.9.1

  2. What operating system and processor architecture are you using?

macOS-12.0.1-arm64-arm-64bit

  1. What are the component versions in the environment (pip freeze)?

asn1crypto==1.4.0 certifi==2021.10.8 cffi==1.15.0 charset-normalizer==2.0.7 cryptography==3.4.8 idna==3.3 numpy==1.21.4 oscrypto==1.2.1 pandas==1.3.4 pyarrow==5.0.0 pycparser==2.21 pycryptodomex==3.11.0 PyJWT==2.3.0 pyOpenSSL==20.0.1 python-dateutil==2.8.2 pytz==2021.3 requests==2.26.0 six==1.16.0 snowflake-connector-python==2.7.1 urllib3==1.26.7

  1. What did you do?

import pandas as pd import snowflake.connector

conn = snowflake.connector.connect( user='', password='', account='', warehouse='', database='', schema='' )

cur = conn.cursor()

sql = "select id from <TABLE_NAME>" cur.execute(sql)

df = cur.fetch_pandas_all() print(df)

  1. What did you expect to see?

Expected to see dataframe values printed although saw this error

File "lib/python3.9/site-packages/snowflake/connector/cursor.py", line 968, in fetch_pandas_all raise NotSupportedError snowflake.connector.errors.NotSupportedError: Unknown error

Process finished with exit code 1

  1. Can you set logging to DEBUG and collect the logs?

Cannot attach full log since it included confidential info although the error might be happening here

2021-11-22 23:01:56,853 - MainThread cursor.py:616 - execute() - DEBUG - executing SQL/command 2021-11-22 23:01:56,853 - MainThread cursor.py:649 - execute() - DEBUG - binding: [select id from <TABLE_NAME>] with input=[None], processed=[{}] 2021-11-22 23:01:56,854 - MainThread cursor.py:696 - execute() - INFO - query: [select id from <TABLE_NAME>] 2021-11-22 23:01:56,854 - MainThread cursor.py:426 - _execute_helper() - DEBUG - Cannot use arrow result format, fallback to json format

richiejthomas avatar Nov 23 '21 04:11 richiejthomas

Hey @richiejthomas, we are starting to look into what will be needed to support M1 and will provide an update once we have completed our analysis.

sfc-gh-hchaturvedi avatar Dec 02 '21 22:12 sfc-gh-hchaturvedi

@sfc-gh-hchaturvedi I successfully just installed and ran dbt-snowflake on an M1 Macbook Pro using latest snowflake-connector-python master branch. There the pyarrow dependency is no longer a problem (current published latest version has a dependency issue with pyarrow 5).

Could I ask the snowflake team to please publish a patch version from latest master (remember to bump the versio number :) )? I think that will solve everyone's problem.

On a M1 Macbook:

# Create a python 3.9 environment (3.10 did *not* work with dbt, I think there aren't arm wheel for 3.10?)
# I did `conda create --name dbt python=3.9`
# conda activate dbt
pip install https://github.com/snowflakedb/snowflake-connector-python/archive/refs/heads/master.zip # install snowflake-connector-python master
pip install dbt-core
# check `which dbt`, ensure it is the right one, otherwise reactivate environment
# call `dbt`, this should work
pip install dbt-snowflake

dbt deps # fix new dependencies
dbt run -m your_model

Here's the python process during dbt run, red line highlighting that this is native arm. Otherwise it would say "Intel". image

thomasaarholt avatar Dec 05 '21 22:12 thomasaarholt

@sfc-gh-hchaturvedi I'm also facing this issue - just wondering if there is any progress on this?

kzecchini avatar Jan 11 '22 22:01 kzecchini

It seems that version 2.7.4 supports M1

sfc-gh-dseweryn avatar Feb 22 '22 15:02 sfc-gh-dseweryn

I upgraded to 2.7.4 (snowflake-connector-python==2.7.4, pyarrow==6.0.1) and I am still getting the same error:

...
"/.../lib/python3.9/site-packages/snowflake/connector/cursor.py", line 978, in fetch_pandas_all
    raise NotSupportedError
snowflake.connector.errors.NotSupportedError: Unknown error

When I tried to force the use of the arrow_iterator module (setting CAN_USE_ARROW_RESULT_FORMAT to true), I see the following:

ImportError: dlopen(/.../lib/python3.9/site-packages/snowflake/connector/arrow_iterator.cpython-39-darwin.so, 0x0002): tried: '/.../lib/python3.9/site-packages/snowflake/connector/arrow_iterator.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e')), '/usr/local/lib/arrow_iterator.cpython-39-darwin.so' (no such file), '/usr/lib/arrow_iterator.cpython-39-darwin.so' (no such file)

To me it still looks like the arrow_iterator is incompatible with M1 architecture...

Is there anything I need to change/install/reinstall to get this to work?

kzecchini avatar Feb 22 '22 17:02 kzecchini

I don't see pyarrow==6.0.1 anywhere in the requirements/dependencies of snowflake-connector-python nor in the pip list so I cannot tell

sfc-gh-dseweryn avatar Feb 22 '22 17:02 sfc-gh-dseweryn

So I tried uninstalling and installing the snowflake connector via pip (without cache) but that didn't work.

Instead I decided to build from source. I followed the instructions in the readme to build the wheel from the repo and it works perfectly now.

My guess is that the PyPI distribution is not built for ARM? I'm not entirely sure what exactly is uploaded to PyPI.

kzecchini avatar Feb 22 '22 17:02 kzecchini

Also wanted to add that installing via:

pip install git+https://github.com/snowflakedb/snowflake-connector-python.git

also works for M1 instead of the PyPI install.

kzecchini avatar Feb 22 '22 21:02 kzecchini

I first installed via pip from the main branch. Then I uninstalled and installed 2.7.3 and 2.7.4 and it worked just fine for me. Unfortunately I am not too familiar with python toolchain to be sure if I by chance have different dependencies after I installed from main.

sfc-gh-dseweryn avatar Feb 23 '22 11:02 sfc-gh-dseweryn

@sfc-gh-dseweryn are you installing via PyPI? Or building the wheel from source?

kzecchini avatar Feb 23 '22 15:02 kzecchini

A couple of clarifications: If you're trying to install in Docker on an M1 Mac, then you have different requirements than if you were installing on your M1 Mac. We've had trouble with a lot of packages after we began running our docker containers natively on Mac rather than emulating them via Rosetta. Many of those problems are being worked out. The snowflake connector still has some issues:

Installing on Mac, the following fails:

$ pip install snowflake-connector-python[pandas,secure-local-storage]
zsh: no matches found: snowflake-connector-python[pandas,secure-local-storage]

pip install snowflake-connector-python works, but then you get pyarrow errors since you're missing the extras mentioned in the code block.

I do not know how to solve this.

Installing in Docker (Debian linux) on an M1 Mac: The following is crazy, but it works to install snowflake-connector-python: If you're getting the following error:

  File "/root/venv/lib/python3.8/site-packages/snowflake/connector/cursor.py", line 978, in fetch_pandas_all
    raise NotSupportedError
snowflake.connector.errors.NotSupportedError: Unknown error

Try reinstalling snowflake-connector-python at version 2.7.2 by pip install -U snowflake-connector-python==2.7.2.

Starting from an empty Python 3.9 environment, you can get it running like this:

pip install snowflake-connector-python[pandas,secure-local-storage]==2.7.4 --no-use-pep517
pip install -U snowflake-connector-python==2.7.2

thomasaarholt avatar Feb 23 '22 15:02 thomasaarholt

The first time the connector installed successfully for me I used:

pip3.9 install git+git://github.com/snowflakedb/snowflake-connector-python@main

then ran

pip3.9 install snowflake-connector-python==2.7.4

then I uninstalled snowflake-connector-python, updated requirements.txt to have version 2.7.4 and checked ran

pip3.9 install -r requirements.txt

I hope this clarifies everything :)

sfc-gh-dseweryn avatar Feb 23 '22 17:02 sfc-gh-dseweryn

@thomasaarholt So you have to put quotes around the package to install via zsh:

pip install "snowflake-connector-python[pandas,secure-local-storage]"   

When I do this I have the pyarrow dependencies, but they don't appear to be built for ARM when you download from PyPI.

Also I have no problem running debian docker containers with snowflake-connector-python==2.7.4 on M1 mac (using images with the build flag --platform=linux/amd64)

kzecchini avatar Feb 23 '22 18:02 kzecchini

@sfc-gh-dseweryn thanks for the clarification. From a new environment, If you try to install from PyPI with no cache pip install --no-cache snowflake-connector-python[pandas]==2.7.4 - does it work for you?

I'm wondering if the first install from source is cached, and then the other subsequent installs are working from the locally cached wheel instead of going out to PyPI for the wheel.

Because for my M1 mac it seems to only work when I build the wheel locally. When I download from PyPI the wheel doesn't work. I just tested with a new environment both building from source and getting the wheel from PyPI.

Building from source (this works - note the wheel name has arm64 in it):

$ pip install git+https://github.com/snowflakedb/snowflake-connector-python.git                                                                                                                                                                        

Collecting git+https://github.com/snowflakedb/snowflake-connector-python.git
  Cloning https://github.com/snowflakedb/snowflake-connector-python.git to /private/var/folders/zw/40p7ybbx0f77mzwc80_0_0kdzhh2rz/T/pip-req-build-vs2vctki
  Running command git clone -q https://github.com/snowflakedb/snowflake-connector-python.git /private/var/folders/zw/40p7ybbx0f77mzwc80_0_0kdzhh2rz/T/pip-req-build-vs2vctki
  Resolved https://github.com/snowflakedb/snowflake-connector-python.git to commit 0a9c74a8ba348b6664f024bafd97d7d605f1f156
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
...
Building wheels for collected packages: snowflake-connector-python
  Building wheel for snowflake-connector-python (PEP 517) ... done
  Created wheel for snowflake-connector-python: filename=snowflake_connector_python-2.7.4-cp39-cp39-macosx_11_0_arm64.whl size=8858782 sha256=d8e8033664ece7b7ee57dc53610ab36c55cabc715832bda5afeef5e70175bb0e
  Stored in directory: /private/var/folders/zw/40p7ybbx0f77mzwc80_0_0kdzhh2rz/T/pip-ephem-wheel-cache-e4iac3yz/wheels/94/47/dc/99235024092c5237d99fa37600bcc37d0984c6875891bff470
Successfully built snowflake-connector-python
Installing collected packages: snowflake-connector-python
Successfully installed snowflake-connector-python-2.7.4

This wheel doesn't work on M1 from PyPI:

$ pip install --no-cache "snowflake-connector-python[pandas]==2.7.4"

Collecting snowflake-connector-python[pandas]==2.7.4
  Downloading snowflake_connector_python-2.7.4-cp39-cp39-macosx_10_14_universal2.whl (10.3 MB)

So you can see that when building from source we have: snowflake_connector_python-2.7.4-cp39-cp39-macosx_11_0_arm64.whl vs. when installing from PyPI we get snowflake_connector_python-2.7.4-cp39-cp39-macosx_10_14_universal2.whl.

Maybe it is an OS issue? MacOS 11.0 vs MacOS 10.14? But I am not sure if the universal wheel is actually universal for M1. I think that this is the underlying issue, and there needs to be a wheel in PyPI which is compatible with M1.

kzecchini avatar Feb 23 '22 18:02 kzecchini

@kzecchini god, that's an unfortunate need of quotes! Thanks for pointing that out!

I've noticed the M1 arm vs universal issue too. I know that pip will always prefer specific wheels over universal wheels if available. I notice the arm ones are not available on pypi.

If you force building the wheels, then it will work on M1 mac: pip install 'snowflake-connector-python[pandas,secure-local-storage]' --no-binary snowflake-connector-python

thomasaarholt avatar Feb 23 '22 20:02 thomasaarholt

I successfully run sql queries into pandas using the following dockerfile:

# Store in a file with filename "Dockerfile"
FROM python:3.9-slim-buster

# required to build pycryptodomex, which is required by secure-local-storage
RUN apt-get update && apt-get install -y gcc g++ 

RUN pip install 'snowflake-connector-python[pandas,secure-local-storage]' --no-binary snowflake-connector-python


COPY script.py /script.py

CMD ["sh", "-c", "python script.py"]

Where script.py contains the following snowflake sql query (replace your own variables), and is in the same folder as the Dockerfile above.

from snowflake.connector import connect

con = connect(
    user=my_username,
    password=my_password,
    account=sql_account,
)
df = con.cursor().execute("select * from database.schema.my_table limit 10").fetch_pandas_all()
print("It worked!")
print(df)

Run as

docker build -t snow . docker run snow

thomasaarholt avatar Feb 23 '22 20:02 thomasaarholt

@thomasaarholt Thanks for the info! I just verified that adding the --no-binary works as well for me. I never used that before - thanks for pointing that out!

So it just seems like the solution for PyPI would be to get the ARM distribution uploaded so folks could easily install a prebuilt wheel that works on M1.

But the workaround for now is to either install from source, or to use the --no-binary flag when installing via PyPI.

kzecchini avatar Feb 23 '22 21:02 kzecchini

@thomasaarholt Thanks for the workaround using the --no-binary flag! That works perfectly.

richiejthomas avatar Feb 23 '22 21:02 richiejthomas

Annoyingly, the above will not build using google clouds docker using buildx to build multi-arch images (specifically building an arm image on x86_64). It fails with a qemu fatal error.

thomasaarholt avatar Feb 24 '22 14:02 thomasaarholt

Hi,

I was facing the same error (M1 mac), and this thread really helped me out. The suggestion of using --non-binary option with pip worked for me. So, Thank you so much for that @thomasaarholt.

However, during this, I discovered another issue, although after spending some time debugging it. I kinda find a solution for it, but I'm still not sure why that error was happening. I thought it would be a good idea to put it here, in case of some face the same issue as me, it might help them. So, someone smarter and well-versed, in this topic, than me (like @thomasaarholt ) might be able to explain to me why it was happening.

So, after you use the --no-binary option posted above, snowflake-connector-python will work as expected, however, if you install certain libraries after this, for example, jupyterlab, you will again start getting the error:

MemoryError: Cannot allocate write+execute memory for ffi.callback(). You might be running on a system that prevents this. For more information, see https://cffi.readthedocs.io/en/latest/using.html#callbacks

This is the same error I was getting while trying to run snowflake.connector.connect function when I installed snowflake-connector-python using simply conda install snowflake-connector-python and went away when I installed using the pip with --no-binary option.

Just to give the EXACT steps I followed, in case, someone wants to recreate it:

conda create -n XYZ
conda activate XYZ

conda install numpy pandas=1.35 matplotlib

conda install pycryptodomex

pip install 'snowflake-connector-python[pandas,secure-local-storage]' --no-binary snowflake-connector-python

At this point, running snowflake.connector.connect gives no error, and run as expected.

conda install jupterlab

Everything here installs perfectly but running snowflake.connector.connect gives the Memory error I mentioned above.

After debugging it for some time, I figure out the cause of it. Basically, it is not jupyterlab (or other similar packages) that is causing the issue, but one of its dependencies - CFFI (https://cffi.readthedocs.io/en/latest/).

CFFI is installed with snowflake-connector-python as one of it's dependencies and the package works perfectly, but when you install jupyterlab conda installs cffi again (as we know conda doesn't check for packages installed by PyPI). You can recreate the same error just by doing conda install cffi right after you install the snowflake connector with pip --no-binary option.

Work around I found, just install jupyterlab or any other package which has cffi as it's dependency through PYPI in the conda virtual environment.

However, I'm still confused why does cffi installation from PyPI works, but from Conda doesn't, even though conda-forge has osx-arm compatible installer for cffi. My guess is - this dependency is the same reason why conda install snowflake-connector-python doesn't work despite it having an osx-arm compatible installer.

I hope it helps anyone who might struggle with this issue, or if someone can tell me why this issue is happening in the first place!

yuvrajsharma9981 avatar Mar 22 '22 03:03 yuvrajsharma9981