ImportError: cannot import name 'check_peft_version' from 'transformers.utils' (/usr/local/lib/python3.10/dist-packages/transformers/utils/__init__.py)
Author: medhanshrath-t
Pull request: no

### System Info

'transformers==4.33.0', 'torch==2.0.1+cu118'
I am running it on Google Colab

### 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, ...)
- [x] My own task or dataset (give details below)

### Reproduction

1. 
```
from transformers import (
    AutoConfig,
    AutoTokenizer,
    AutoModelForCausalLM,
    AutoModelForSequenceClassification,
    BitsAndBytesConfig,
    DataCollatorForLanguageModeling,
    DataCollatorForSeq2Seq,
    Trainer,
    TrainingArguments,
    GenerationConfig,
    pipeline
)
```

2. 
```
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py](https://localhost:8080/#) in _get_module(self, module_name)
   1116         try:
-> 1117             return importlib.import_module("." + module_name, self.__name__)
   1118         except Exception as e:

13 frames
[/usr/lib/python3.10/importlib/__init__.py](https://localhost:8080/#) in import_module(name, package)
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 

/usr/lib/python3.10/importlib/_bootstrap.py in _gcd_import(name, package, level)

/usr/lib/python3.10/importlib/_bootstrap.py in _find_and_load(name, import_)

/usr/lib/python3.10/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

/usr/lib/python3.10/importlib/_bootstrap.py in _load_unlocked(spec)

/usr/lib/python3.10/importlib/_bootstrap_external.py in exec_module(self, module)

/usr/lib/python3.10/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

[/usr/local/lib/python3.10/dist-packages/transformers/trainer.py](https://localhost:8080/#) in <module>
     39 # Integrations must be imported before ML frameworks:
---> 40 from .integrations import (  # isort: split
     41     default_hp_search_backend,

[/usr/local/lib/python3.10/dist-packages/transformers/integrations/__init__.py](https://localhost:8080/#) in <module>
     70 )
---> 71 from .peft import PeftAdapterMixin

[/usr/local/lib/python3.10/dist-packages/transformers/integrations/peft.py](https://localhost:8080/#) in <module>
     16 
---> 17 from ..utils import (
     18     check_peft_version,

ImportError: cannot import name 'check_peft_version' from 'transformers.utils' (/usr/local/lib/python3.10/dist-packages/transformers/utils/__init__.py)

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
[<ipython-input-17-0c98504d1a0a>](https://localhost:8080/#) in <cell line: 5>()
      3 import torch
      4 import datasets
----> 5 from transformers import (
      6     AutoConfig,
      7     AutoTokenizer,

/usr/lib/python3.10/importlib/_bootstrap.py in _handle_fromlist(module, fromlist, import_, recursive)

[/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py](https://localhost:8080/#) in __getattr__(self, name)
   1105             value = self._get_module(name)
   1106         elif name in self._class_to_module.keys():
-> 1107             module = self._get_module(self._class_to_module[name])
   1108             value = getattr(module, name)
   1109         else:

[/usr/local/lib/python3.10/dist-packages/transformers/utils/import_utils.py](https://localhost:8080/#) in _get_module(self, module_name)
   1117             return importlib.import_module("." + module_name, self.__name__)
   1118         except Exception as e:
-> 1119             raise RuntimeError(
   1120                 f"Failed to import {self.__name__}.{module_name} because of the following error (look up to see its"
   1121                 f" traceback):\n{e}"

RuntimeError: Failed to import transformers.trainer because of the following error (look up to see its traceback):
cannot import name 'check_peft_version' from 'transformers.utils' (/usr/local/lib/python3.10/dist-packages/transformers/utils/__init__.py)
```

### Expected behavior

The import worked a few days ago, but it is not working now.
Duplicate of now closed issue #26065 

Comments:
cc @younesbelkada @BenjaminBossan maybe

Hi @medhanshrath-t 
Hmm I have just spined a Google Colab instance
<img width="822" alt="Screenshot 2023-09-28 at 15 49 01" src="https://github.com/huggingface/transformers/assets/49240599/061271cb-fd1c-48c9-9863-a9a362d6ac6c">

And seems to work fine, I think the issue is that you have a package conflict with transformers installed from source and from pypi. Can you try again on a fresh new environment?

I tried installing only `transformers==4.33.0` and it works fine. I am getting the error when I am installing all the packages I need:

`!pip install -U -q transformers adapter-transformers datasets peft accelerate bitsandbytes safetensors evaluate tyro trl`

Indeed, I have managed to reproduce, 
when calling
```bash
!pip install -U -q transformers adapter-transformers datasets peft accelerate bitsandbytes safetensors evaluate tyro trl
```
One of the library after transformers overrides the newest version of transformers (4.33.3) with an old one `4.26.1`, and it seems to create some package conflicts leading to the error. 

And running your script with a standalone install to 4.26.1 leads to:
![Screenshot 2023-09-29 at 13 13 18](https://github.com/huggingface/transformers/assets/49240599/6b588bfa-7e23-46df-9a3e-157191bad9f5)

```
ImportError                               Traceback (most recent call last)
[<ipython-input-2-d8b21716d2a0>](https://localhost:8080/#) in <cell line: 1>()
----> 1 from transformers import (
      2     AutoConfig,
      3     AutoTokenizer,
      4     AutoModelForCausalLM,
      5     AutoModelForSequenceClassification,

ImportError: cannot import name 'BitsAndBytesConfig' from 'transformers' (/usr/local/lib/python3.10/dist-packages/transformers/__init__.py)
```

Since some objects have been added only later. So for me there is a conflict in your env causing the issue, the fix in your case is to simply uninstall transformers and re-install it


According to pip list the version of transformer is 4.33.3. I tried uninstalling and reinstalling transformers and I still have the same error.
```
!pip install -q -U transformers adapter-transformers datasets peft accelerate bitsandbytes safetensors evaluate tyro trl
!pip uninstall -q transformers
!pip install -q -U transformers
```

Correction: Tried it again and it worked, not sure why it didn't work previously. Is there a long term solution for this?

Hmmm I can't really tell, from what I can see installing transformers on a fresh env and importing the objects you shared works fine. I definitely think in your case it is a package conflict issue that can be solved only by re-installing transformers after removing the broken one.

Ok, thank you for your help

@medhanshrath-t  as mentioned by @younesbelkada :
> One of the library after transformers overrides the newest version of transformers (4.33.3) with an old one `4.26.1`, and it seems to create some package conflicts leading to the error.

That library is `adapter-transformers` **(⚠️ deprecated)**,  as indicated in the documentation:

> `adapter-transformers` is a direct fork of `transformers`. This means our package includes all the awesome features of HuggingFace’s original package, plus the adapter implementation. As both packages share the same namespace, they ideally should not be installed in the same environment.

This library is **deprecated** and replaced by  [adapters](https://docs.adapterhub.ml/), that can be installed  in the same environment with `transformers`.

installing `adapters` instead of `adapter-transformers` will solve your problem:

```
!pip install -U -q transformers adapters  datasets peft accelerate bitsandbytes safetensors evaluate tyro trl
```