ImportError: DLL load failed while importing _safetensors_rust: The specified module could not be found
Author: JamasChuang94
Pull request: no
Labels: name: bug

### System Info


- `transformers` version: 4.52.3
- Platform: Windows-11-10.0.26100-SP0
- Python version: 3.13.2
- Huggingface_hub version: 0.31.2
- Safetensors version: 0.5.3
- Accelerate version: 1.7.0
- Accelerate config:    not found
- DeepSpeed version: not installed
- PyTorch version (GPU?): 2.7.0+xpu (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?: <fill in>
- Using XPU in script?: <fill in>
- XPU type: Intel(R) Arc(TM) A770 Graphics

### 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

1. It works fine in the python command line, but the dynamic library cannot be loaded in CPython

`import torch
import intel_extension_for_pytorch as ipex
from transformers import T5ForConditionalGeneration, T5Tokenizer

print('Hi')`

2. The following code will result in the following error 2.1
 ```#include <iostream>
 #include<windows.h>
 #include "Python.h"
 
 int main() {
 
     if (!SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) {
         std::cerr << "SetDefaultDllDirectories  fail" << std::endl;
         return 1;
     }
 
     PyStatus status;
     PyConfig config;
     PyConfig_InitPythonConfig(&config);
     wchar_t* home_path = Py_DecodeLocale("C:/Users/username/anaconda3/envs/Pytorch-ipx", NULL);
     if (home_path == NULL) {
         fprintf(stderr, "Py_DecodeLocale fail\n");
         return 1;
     }
 
     config.home = home_path;
     status = Py_InitializeFromConfig(&config);
     if (PyStatus_Exception(status)) {
         fprintf(stderr, "Python initialization failed\n");
         PyConfig_Clear(&config);
         PyMem_RawFree(home_path);
         return 1;
     }

     PyConfig_Clear(&config);

     PyRun_SimpleString("import torch; import time; import intel_extension_for_pytorch as ipex; from transformers import T5ForConditionalGeneration, T5Tokenizer; print('Hi')");
 
     Py_Finalize();
     PyMem_RawFree(home_path);

     return 0;
 }
```
2.1 Errors
```
from transformers import T5ForConditionalGeneration, T5Tokenizer
  File "C:\Users\zhang\anaconda3\envs\Pytorch-ipx\Lib\site-packages\transformers\utils\import_utils.py", line 2045, in __getattr__
    module = self._get_module(self._class_to_module[name])
  File "C:\Users\zhang\anaconda3\envs\Pytorch-ipx\Lib\site-packages\transformers\utils\import_utils.py", line 2075, in _get_module
    raise e
  File "C:\Users\zhang\anaconda3\envs\Pytorch-ipx\Lib\site-packages\transformers\utils\import_utils.py", line 2073, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\zhang\anaconda3\envs\Pytorch-ipx\Lib\importlib\__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\zhang\anaconda3\envs\Pytorch-ipx\Lib\site-packages\transformers\models\t5\modeling_t5.py", line 28, in <module>
    from ...cache_utils import Cache, DynamicCache, EncoderDecoderCache
  File "C:\Users\zhang\anaconda3\envs\Pytorch-ipx\Lib\site-packages\transformers\cache_utils.py", line 12, in <module>
    from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_6
  File "C:\Users\zhang\anaconda3\envs\Pytorch-ipx\Lib\site-packages\transformers\pytorch_utils.py", line 21, in <module>
    from safetensors.torch import storage_ptr, storage_size
  File "C:\Users\zhang\anaconda3\envs\Pytorch-ipx\Lib\site-packages\safetensors\__init__.py", line 2, in <module>
    from ._safetensors_rust import (  # noqa: F401
    ...<6 lines>...
    )
ImportError: DLL load failed while importing _safetensors_rust: The specified module could not be found.
```


### Expected behavior

Works just like in the python command line

Comments:
Hi @JamasChuang94, the issue is likely in the `safetensors` library rather than `transformers`, but I don't think we guarantee that kind of CPython hackery! You're probably on your own with it, but if you can figure out the issue we might accept a PR to fix it, if it doesn't break anything else.

@JamasChuang94 请问这个问题你最后解决了吗？我也遇到相同问题了

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.