ImportError: cannot import name 'pipeline' from 'transformers'
Author: atabari-bci
Pull request: no
Labels: name: Usage

name: bug

### System Info

I am using Databricks notebook. 
Databricks runtime: 13.3 LTS (includes Apache Spark 3.4.1, Scala 2.12)

### Who can help?

@Rocketknight1 @SunMarc @zach-huggingface

### Information

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

### Tasks

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

### Reproduction

Here is the code:

```
%pip install --upgrade torch transformers accelerate deepspeed bitsandbytes huggingface_hub
dbutils.library.restartPython()

import os
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
```

Error:
`ImportError: cannot import name 'pipeline' from 'transformers' (/local_disk0/.ephemeral_nfs/envs/pythonEnv-a13cd5c4-d035-4d04-87bd-75088348617d/lib/python3.10/site-packages/transformers/__init__.py)`

Python: 3.10.12
installed packages:
transformers== 4.53.0
huggingface_hub==0.33.1
torch==2.7.1+cu126
accelerate==1.8.1
deepspeed==0.17.1
bitsandbytes==0.46.0

These are all up-to-date versions for all of these packages. What is the problem?

### Expected behavior

Import without error.

Comments:
FYI, changed trnsformer version to transformers==4.51.3 and no error.

Hey!
In my case, the installed version of `transformers` was `4.52.4`, which does not exist on the official [PyPI repository](https://pypi.org/project/transformers/) and appears to be either an unofficial or broken version.
```
nano /home/.../miniconda3/envs/.../lib/python3.10/site-packages/transformers/__init__.py

```
I noticed the pipeline function was not imported (only the Pipeline class with a **capital P** was exposed under from .pipelines import (...)), confirming the source of the failure.
Reinstalling the correct version solved the problem:
```
pip uninstall transformers tokenizers -y
pip install transformers==4.41.2 tokenizers==0.19.1
```

Try doing this:
<pre>
import transformers
print(transformers.__file__)
</pre>
If file exists
<pre>
import inspect
print(inspect.getsource(transformers))
</pre>
If you are unable to find pipeline in the source then this means your package is broken. Try reinstalling it.
<pre>
pip uninstall transformers tokenizers -y
pip install transformers==4.41.2 tokenizers==0.19.1
</pre>


This seems unrelated to `transformers`! 

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.

Same error on v4.55.0

OS: Ubuntu 22.04
Python: 3.10.9
installed packages:
transformers== 4.55.0
huggingface_hub==0.34.3
torch==2.7.1

Sorry I have to close this it is not reproducible!

Hi @ArthurZucker 
The above issue is reproducible if you are on huggingface version 4.55.2

Doing pip install transformers install the 4.55.2 version which has the error : 
ImportError: cannot import name 'pipeline' from 'transformers'

<img width="730" height="273" alt="Image" src="https://github.com/user-attachments/assets/98c0c457-7ddd-4d28-95bc-1219f44cd348" />

I cannot import pipeline either, using 4.55.2

Sorry but you most probably have an issue with your environment 

Has anyone solved this issue somehow?

I have the problem too. 
I litterally only created a new Lambdalabs A100 gpu notebook with no storage associated to it and I only installed transformers. 

I runned :
!pip install transformers
then 
!pip install git+https://github.com/huggingface/transformers


@ArthurZucker

I found the problem: if one of the imported modules raises an `AttributeError` exception during a lazy import, the exception is silently ignored by the `_LazyModule` in [src/transformers/utils/import_utils.py](https://github.com/huggingface/transformers/blob/87be5595081364ef99393feeaa60d71db3652679/src/transformers/utils/import_utils.py#L1935), because it catches only `ModuleNotFoundError` and `RuntimeError`, but doesn't catch other exceptions like `AttributeError`.

I had pillow==9.0.1, which doesn't have the PIL.Image.Resampling attibute and raises an `AttibuteError` (which is silently ignored).

I had a similar problem before with a different import. I've seen people reporting this problem for a very long time.

Similar issue was raised in 2021 in https://github.com/huggingface/transformers/issues/13007 and https://github.com/huggingface/transformers/pull/14124, partially fixed in https://github.com/huggingface/transformers/pull/14124/commits/60bfc112e549004f732979e94d22b57e9e79cd7a.

Recently a try-except was added by @LysandreJik to `import_utils.py`, which catches other `Exceptions`, line 2028: https://github.com/huggingface/transformers/commit/23d79cea75f0be1d38f4e3d468c52e79012ab179

Maybe we just catch all `Exceptions`, or at least the `AttributeError`?

For people who have this issue:

1. Check what fails:

```
import transformers
print(transformers.pipeline)
```

or:

```
import importlib
importlib.import_module('transformers.pipelines')
```

2. If PIL is the problem, try upgrading:

`pip install -U pillow`