5

I'm coniststently getting this error when trying to use scipy or sklearn packages in Python3.9 via a Jupyter notebook.

The error is:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~/opt/anaconda3/lib/python3.9/site-packages/scipy/__init__.py in <module>
    165     try:
--> 166         from scipy._lib._ccallback import LowLevelCallable
    167     except ImportError as e:

~/opt/anaconda3/lib/python3.9/site-packages/scipy/_lib/_ccallback.py in <module>
----> 1 from . import _ccallback_c
      2 
      3 import ctypes

ImportError: dlopen(/Users/dn/opt/anaconda3/lib/python3.9/site-packages/scipy/_lib/_ccallback_c.cpython-39-darwin.so, 0x0002): tried: '/Users/dn/opt/anaconda3/lib/python3.9/site-packages/scipy/_lib/_ccallback_c.cpython-39-darwin.so' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))

The above exception was the direct cause of the following exception:

ImportError                               Traceback (most recent call last)
/var/folders/d7/q_fznsr95_97r6lp_mx_vp640000gn/T/ipykernel_37825/912110602.py in <module>
      1 #import scipy
----> 2 from sklearn.ensemble import RandomForestClassifier #Picks up non-linearities

~/opt/anaconda3/lib/python3.9/site-packages/sklearn/__init__.py in <module>
     80     from . import _distributor_init  # noqa: F401
     81     from . import __check_build  # noqa: F401
---> 82     from .base import clone
     83     from .utils._show_versions import show_versions
     84 

~/opt/anaconda3/lib/python3.9/site-packages/sklearn/base.py in <module>
     15 from . import __version__
     16 from ._config import get_config
---> 17 from .utils import _IS_32BIT
     18 from .utils._tags import (
     19     _DEFAULT_TAGS,

~/opt/anaconda3/lib/python3.9/site-packages/sklearn/utils/__init__.py in <module>
     20 import warnings
     21 import numpy as np
---> 22 from scipy.sparse import issparse
     23 
     24 from .murmurhash import murmurhash3_32

~/opt/anaconda3/lib/python3.9/site-packages/scipy/__init__.py in <module>
    169               "(extension modules cannot be imported), " + \
    170               "please try reinstalling."
--> 171         raise ImportError(msg) from e
    172 
    173     from scipy._lib._testutils import PytestTester

ImportError: The `scipy` install you are using seems to be broken, (extension modules cannot be imported), please try reinstalling.

I've tried uninstalling and reinstalling scipy both as it describes in the documentation as well as using homebrew but still getting the same error.

Does anyone have an idea how this can be resolved? Scipy was working on my machine a few weeks ago - not sure what has changed in the past weeks.

Any help much appreciated.

Lakeside52
  • 159
  • 1
  • 2
  • 7
  • Based on [here](https://apple.stackexchange.com/q/421486) and [here](https://github.com/apple/tensorflow_macos/issues/21#issuecomment-832139666), I'm guessing you have an M1 Mac? Is Rosetta involved? If so, maybe [here](https://apple.stackexchange.com/a/423324) helps? Whatever machine you have, you should include it in the post. Similar issue with a different package [here](https://stackoverflow.com/a/72811632/8508004) said the solution was to downgrade the Python version & then reinstall the problematic package. [This one](https://stackoverflow.com/a/71249174/8508004) said needed `Amd64`(?)? – Wayne Aug 22 '22 at 20:21
  • Not sure if `Amd64` is a typo for `Arm64` or is correct. Anyway... [Another case where downgrading Python helped](https://github.com/nteract/hydrogen/issues/2147#issue-1178029359). [This example](https://www.reddit.com/r/learnpython/comments/r2ygac/import_error_on_m1_macbook_have_arm64_need_x86_64/) deleted the package involved. reinstalled Python, and then reinstalled the package. – Wayne Aug 22 '22 at 20:25
  • @Wayne thanks for the reply - I do have an M1 mac. Rosetta is not involved. I have looked at related Stack Overflow questions but have not been able to get anything to work. I try to stay away from deleting Python and connected packages as it potentially interferes with other packages for things that are working. But perhaps it's necessary here - in any case I could continue this task using Google Colab as they have the necessary packages available – Lakeside52 Aug 23 '22 at 08:27
  • 1
    You can use virtual environments to install newer Python while keeping your old installation intact. See [here](https://www.freecodecamp.org/news/how-to-setup-virtual-environments-in-python/). It's very useful for testing new versions and organize projects. And actually Anaconda that you seem to be using has an option built in for making environments and you may want to look into that. – Wayne Aug 23 '22 at 12:32
  • 1
    Making environments from the Anaconda Navigator (at the time what I'll link to was written) is one of the steps is to choose the Python version, see [here](https://wiki.math.ntnu.no/anaconda/createenvironment). So maybe setting up a different version and reinstalling the packages you need will be easier than venv since you already have Anaconda. – Wayne Aug 23 '22 at 15:10

2 Answers2

0

I also face the same problem but reinstallation worked for me. Use Anaconda(or Miniconda) to uninstall and install scipy

pip uninstall scipy
pip install scipy

During debugging I also upgrade pip and install spider maybe they contain any package which helped me.

xxx
  • 1,153
  • 1
  • 11
  • 23
0

For me this was due to anti virus, deleting the appdata copy of the scipy file. Check if there is a file _ccallbcak_c.cp310-win_amd64.pyd being blocked from C:\Users\XXXX\AppData\Roaming\Python\Python310\site-packages\scipy_lib