BUG:  Failed to import transformers.models.auto.modeling_auto due to No module named 'scipy.optimize._highspy._core.simplex_constants';
Author: jaheer-hussain
Pull request: no
Labels: name: bug

### System Info

The simple below imports 

 sentence_transformers import SentenceTransformer 
from sklearn.metrics.pairwise import cosine_similarity

are failing as follows.

           RuntimeError: Failed to import transformers.models.auto.modeling_auto because of the following error (look up to see its traceback):
           Failed to import transformers.generation.utils because of the following error (look up to see its traceback):
           No module named 'scipy.optimize._highspy._core.simplex_constants'; 'scipy.optimize._highspy._core' is not a package

Is it related scipy?  This was working fine in python 3.9

Pls find below the environment details.

- 
- `transformers` version: 4.48.0
- Platform: macOS-15.3.1-arm64-arm-64bit
- Python version: 3.11.11
- Huggingface_hub version: 0.29.1
- Safetensors version: 0.5.3
- Accelerate version: not installed
- Accelerate config: not found
- PyTorch version (GPU?): 2.6.0 (False)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using distributed or parallel set-up in script?: distributed


### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity

def test()
        cos_sim = cosine_similarity ( generated_embedded_query, true_embedded_query])

### Expected behavior

Succes

Comments:
Hello @jaheer-hussain you issue indeed looks like its most likely related to SciPy. Check the version for SciPy, Scikit-learn and transformers. 

I tried reproducing the error with env:
```python==3.11.11
scipy==1.15.2
transformers==4.48.0
scikit-learn==1.6.1
torch==2.6.0
```

and follwing code: 
```
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import scipy

try:
    from scipy.optimize._highspy._core import simplex_constants
    print("Module exists")
except ImportError as e:
    print(f"Import Error: {e}")


def test():
    model = SentenceTransformer("all-MiniLM-L6-v2")

    # Create some queries
    generated_query = "What is machine learning?"
    true_query = "Explain machine learning concept"

    # Embed queries
    generated_embedded_query = model.encode([generated_query])
    true_embedded_query = model.encode([true_query])

    # Cosine similarity
    cos_sim = cosine_similarity(generated_embedded_query, true_embedded_query)

    print(f"Cosine Similarity: {cos_sim}")


if __name__ == "__main__":
    test()
```

output: 
```
Module exists
Cosine Similarity: [[0.86462414]]`
```

I think you should check version compatibility of your modules or try creating a fresh environment and installing  just the packages you need `pip install transformers sentence-transformers` . These two modules should install their dependencies with compatible versions of SciPy and scikit-learn, or you can pin the versions similar to me to resolve your issue. 

Hope this works ! 

Thank you so much for the reply. Yes. We are encountering this issue in unit test. Somewhere the patch was not using proper version. Looking into it. Thanks for the clarification 

Closing since this seems like an environment issue, feel free to reopen if you think there's something we can fix in transformers!