Bug of check_imports
Author: wqh17101
Pull request: no

### System Info

- `transformers` version: 4.30.2
- Platform: Linux-4.15.0-213-generic-x86_64-with-glibc2.27
- Python version: 3.9.18
- Huggingface_hub version: 0.17.3
- Safetensors version: 0.4.0
- PyTorch version (GPU?): 2.0.1+cpu (False)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>


### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [X] 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

My huggingface repo is https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat
when i modify [modeling_baichuan.py](https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat/blob/main/modeling_baichuan.py)
change
```
from .configuration_baichuan import BaichuanConfig
from .generation_utils import build_chat_input, TextIterStreamer
```
to 
```
try:
  from .configuration_baichuan import BaichuanConfig
  from .generation_utils import build_chat_input, TextIterStreamer
except:
  from configuration_baichuan import BaichuanConfig
  from generation_utils import build_chat_input, TextIterStreamer
```
to support using the model in the model dir directly.
And get `ImportError: This modeling file requires the following packages that were not found in your environment: configuration_baichuan, generation_utils. Run `pip install configuration_baichuan generation_utils`




### Expected behavior

I don't know why we need to check_imports  by transformers instead of Python own.

I expect no error for my script for , it is equal to the original script 

Comments:
```python
import re
content="""
try:
    from .configuration_baichuan import BaichuanConfig
    from .generation_utils import build_chat_input, TextIterStreamer
except:
    from configuration_baichuan import BaichuanConfig
    from generation_utils import build_chat_input, TextIterStreamer
"""
content = re.sub(r"\s*try\s*:\s*.*?\s*except\s*.*?:", "", content, flags=re.MULTILINE | re.DOTALL)
print(content)
```
```
    from configuration_baichuan import BaichuanConfig
    from generation_utils import build_chat_input, TextIterStreamer
```
as you can see,it only removes the thing in try block

Hi @wqh17101, thanks for raising an issue! 

Am I right in understanding [this example](https://github.com/huggingface/transformers/issues/27554#issuecomment-1816304371) is referring to [this line](https://github.com/huggingface/transformers/blob/638d49983f36af910934b38771b4e55c835c1774/src/transformers/dynamic_module_utils.py#L148) in the code? 

> I expect no error for my script for , it is equal to the original script

Without knowing what code you're running, the full error message or where the script is relative to the modeling files we can only guess the issue. Could you provide a code snippet of what's being run and where relative to the model repo? Could you link to the "original" script? 

What `check_imports` is doing is seeing whether the specified module [can be imported](https://github.com/huggingface/transformers/blob/638d49983f36af910934b38771b4e55c835c1774/src/transformers/dynamic_module_utils.py#L174C9-L174C9). This can be [relative or absolute](https://docs.python.org/3/library/importlib.html#importlib.import_module): if an error is being raised then it indicates `configuration` can not be imported. 

@amyeroberts Thank you.After I read your the source code, I think we do not need the real script.

Because the line you found is aimed to remove the content both in try and except block. But now, it only removes try block.


Also let's think about transformers without check_imports. if some modules cannot be imported,python will raise the error originally.

With check_imports, you will raise the error by using importlib. I think this is superfluous.

On the other hand, python is a dynamic language, you can't check the modules to be import statically by text. Because there can be too many flexible conditional branch grammar like try...except... if...else.... and so on. You can see that the source code doesn't consider the grammar if....else....

@wqh17101 Yes, it's true that handling imports in python is messy, particularly considering if/else, try/except blocks. To provide some context, one of the reason for `check_imports` is to provide clear error messages for our users. Within the library there's many additional packages which can optionally be installed for certain functionality e.g. Pillow for image processing. This explicitly tells the user they need to install that package.

@amyeroberts Before there is a good way to check, I think it is better for you to provide some options to let user skip this check

@wqh17101 If there's a particular solution you have in mind, feel free to open up a PR and ping me for review! 

> @amyeroberts Before there is a good way to check, I think it is better for you to provide some options to let user skip this check

What about this one? You want me to PR about this?

@wqh17101 What I'm suggesting is that if this is something you think should be addressed and you have a solution in mind then you can open a PR directly and ask for a review from myself (or another relevant person working at Hugging Face). 

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.