Importing transformers in ipython throws error due to `_pytest`
Author: stephantul
Pull request: no

### System Info

- `transformers` version: 4.30.1
- Platform: macOS-12.5-arm64-arm-64bit
- Python version: 3.10.8
- Huggingface_hub version: 0.15.1
- Safetensors version: 0.3.1
- PyTorch version (GPU?): 2.0.1 (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?: no
- Using distributed or parallel set-up in script?: no

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

typing `from transformers.` and then pressing `TAB` (i.e., autocompletion) shows the following stack trace in `ipython`. Aftwards, everything works as expected, although trying to use anything from `transformers.testing_utils` will throw the same error. This happens because `ipython` is doing introspection of the testing module, which then attempts to import the `_pytest` module, which doesn't exist. 

```
Traceback (most recent call last):
  File "my_env/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1084, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
  File "/opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "my_env/lib/python3.10/site-packages/transformers/testing_utils.py", line 39, in <module>
    from _pytest.doctest import (
ModuleNotFoundError: No module named '_pytest'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "my_env/lib/python3.10/site-packages/IPython/core/completer.py", line 3171, in _complete
    result = matcher(context)
  File "my_env/lib/python3.10/site-packages/IPython/core/completer.py", line 2707, in custom_completer_matcher
    matches = self.dispatch_custom_completer(context.token) or []
  File "my_env/lib/python3.10/site-packages/IPython/core/completer.py", line 2747, in dispatch_custom_completer
    res = c(event)
  File "my_env/lib/python3.10/site-packages/IPython/core/completerlib.py", line 272, in module_completer
    return module_completion(event.line)
  File "my_env/lib/python3.10/site-packages/IPython/core/completerlib.py", line 249, in module_completion
    completion_list = try_import('.'.join(mod[:-1]), True)
  File "my_env/lib/python3.10/site-packages/IPython/core/completerlib.py", line 183, in try_import
    completions.extend( [attr for attr in dir(m) if
  File "my_env/lib/python3.10/site-packages/IPython/core/completerlib.py", line 184, in <listcomp>
    is_importable(m, attr, only_modules)])
  File "my_env/lib/python3.10/site-packages/IPython/core/completerlib.py", line 153, in is_importable
    return inspect.ismodule(getattr(module, attr))
  File "my_env/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1072, in __getattr__
    value = self._get_module(name)
  File "my_env/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1086, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.testing_utils because of the following error (look up to see its traceback):
No module named '_pytest'
```


### Expected behavior

A possible workaround would be to use a try-except statement around this block, which then prints a useful error message.

Comments:
I have exactly same issue with this, how to resolve the issue? 

I have the same issue with the 4.30.0 version. Try to use the 4.29.0 version.

We can workaround it with `pip install pytest`

@gkgkska @xxupiano @xin3he Thanks for reporting this. A fix has now been merged into `main`. 

Running into a similar error when running `4.31.0.dev0` transformer language modeling example. 

```(base) ubuntu@104-171-202-20:~/llm-training/transformers/examples/pytorch/language-modeling$ python run_clm.py     --model_name_or_path gpt2     --dataset_name wikitext     --dataset_config_name wikitext-2-raw-v1     --per_device_train_batch_size 8     --per_device_eval_batch_size 8     --do_train     --do_eval     --output_dir /tmp/test-clm
Traceback (most recent call last):
  File "/home/ubuntu/llm-training/transformers/examples/pytorch/language-modeling/run_clm.py", line 51, in <module>
    from transformers.testing_utils import CaptureLogger
  File "/home/ubuntu/anaconda3/lib/python3.9/site-packages/transformers/testing_utils.py", line 109, in <module>
    from _pytest.doctest import (
ImportError: cannot import name 'Module' from '_pytest.doctest' (/home/ubuntu/anaconda3/lib/python3.9/site-packages/_pytest/doctest.py)```

Hi @praateekmahajan, could you provide some more information about the issue? Specifically a reproducible set of steps or code and information about the running environment (run `transformers-cli env` in your terminal). 

> Running into a similar error when running `4.31.0.dev0` transformer language modeling example.
> 
> ```
> Traceback (most recent call last):
>   File "/home/ubuntu/llm-training/transformers/examples/pytorch/language-modeling/run_clm.py", line 51, in <module>
>     from transformers.testing_utils import CaptureLogger
>   File "/home/ubuntu/anaconda3/lib/python3.9/site-packages/transformers/testing_utils.py", line 109, in <module>
>     from _pytest.doctest import (
> ImportError: cannot import name 'Module' from '_pytest.doctest' (/home/ubuntu/anaconda3/lib/python3.9/site-packages/_pytest/doctest.py)```
> ```

I am also seeing this error. I just hacked out the use of the CapturLogger in the example run_clm.

Since this bug is closed, should we open a different one with this issue?

@asampat3090 @sei-amellinger 

@amyeroberts 's fix #24241 is merged after the tag `v4.31.0.dev0`. So the first thing to check is to see what the commit you have used to install `transformers`. In any case, you can fetch the latest commit and install it again. It should work fine I think. 

pip3 uninstall pytest
pip3 install pytest

maybe pytest version is old

> maybe pytest version is old

Yeah, it was on old version of pytest.

Thanks!

Hi team, when will there be a new release that contains the fix from https://github.com/huggingface/transformers/pull/24241?

cc @sgugger

The next release will be early next week, probably on Tuesday.

Thank you!