Failed to import transformers.trainer
Author: minji-o-j
Pull request: no

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->

- `transformers` version: '4.13.0'
- Platform: Linux
- Python version: 3.8.12
- PyTorch version (GPU?): '1.10.0+cu113'
- I use a tensorrt docker `docker pull nvcr.io/nvidia/pytorch:21.11-py3`

### Who can help
@sgugger

<!-- Your issue will be replied to more quickly if you can figure out the right person to tag with @
 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- ALBERT, BERT, XLM, DeBERTa, DeBERTa-v2, ELECTRA, MobileBert, SqueezeBert: @LysandreJik
- T5, BART, Marian, Pegasus, EncoderDecoder: @patrickvonplaten
- Blenderbot, MBART: @patil-suraj
- Longformer, Reformer, TransfoXL, XLNet, FNet, BigBird: @patrickvonplaten
- FSMT: @stas00
- Funnel: @sgugger
- GPT-2, GPT: @patrickvonplaten, @LysandreJik
- RAG, DPR: @patrickvonplaten, @lhoestq
- TensorFlow: @Rocketknight1
- JAX/Flax: @patil-suraj
- TAPAS, LayoutLM, LayoutLMv2, LUKE, ViT, BEiT, DEiT, DETR, CANINE: @NielsRogge
- GPT-Neo, GPT-J, CLIP: @patil-suraj
- Wav2Vec2, HuBERT, SpeechEncoderDecoder, UniSpeech, UniSpeechSAT, SEW, SEW-D, Speech2Text: @patrickvonplaten, @anton-l

If the model isn't in the list, ping @LysandreJik who will redirect you to the correct contributor.

Library:

- Benchmarks: @patrickvonplaten
- Deepspeed: @stas00
- Ray/raytune: @richardliaw, @amogkam
- Text generation: @patrickvonplaten @narsil
- Tokenizers: @LysandreJik
- Trainer: @sgugger
- Pipelines: @Narsil
- Speech: @patrickvonplaten, @anton-l
- Vision: @NielsRogge, @sgugger

Documentation: @sgugger

Model hub:

- for issues with a model, report at https://discuss.huggingface.co/ and tag the model's creator.

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj

For research projetcs, please ping the contributor directly. For example, on the following projects:

- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 -->

## Information

Model I am using (Bert, XLNet ...):

The problem arises when using:
* the official example scripts: (give details below)

The tasks I am working on is:
* my own task or dataset: (give details below)

```py
from transformers import Trainer
```

## To reproduce

Steps to reproduce the behavior:

1. make python file
```py
from transformers import Trainer
```

2. run it


<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior
```
Traceback (most recent call last):
  File "/opt/conda/lib/python3.8/site-packages/transformers/file_utils.py", line 2281, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
  File "/opt/conda/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/opt/conda/lib/python3.8/site-packages/transformers/trainer.py", line 147, in <module>
    from apex import amp
  File "/opt/conda/lib/python3.8/site-packages/apex/__init__.py", line 20, in <module>
    from . import transformer
  File "/opt/conda/lib/python3.8/site-packages/apex/transformer/__init__.py", line 3, in <module>
    from apex.transformer import pipeline_parallel
  File "/opt/conda/lib/python3.8/site-packages/apex/transformer/pipeline_parallel/__init__.py", line 1, in <module>
    from apex.transformer.pipeline_parallel.schedules import get_forward_backward_func
  File "/opt/conda/lib/python3.8/site-packages/apex/transformer/pipeline_parallel/schedules/__init__.py", line 4, in <module>
    from apex.transformer.pipeline_parallel.utils import get_num_microbatches
  File "/opt/conda/lib/python3.8/site-packages/apex/transformer/pipeline_parallel/utils.py", line 23, in <module>
    import amp_C
ImportError: /opt/conda/lib/python3.8/site-packages/amp_C.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZN3c105ErrorC2ENS_14SourceLocationENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "trt_infer.py", line 2, in <module>
    from transformers import Trainer
  File "<frozen importlib._bootstrap>", line 1039, in _handle_fromlist
  File "/opt/conda/lib/python3.8/site-packages/transformers/file_utils.py", line 2271, in __getattr__
    module = self._get_module(self._class_to_module[name])
  File "/opt/conda/lib/python3.8/site-packages/transformers/file_utils.py", line 2283, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.trainer because of the following error (look up to see its traceback):
/opt/conda/lib/python3.8/site-packages/amp_C.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZN3c105ErrorC2ENS_14SourceLocationENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
```
<!-- A clear and concise description of what you would expect to happen. -->


Comments:
Seems like there is a problem in your docker container with the apex library, as the error actually comes when trying to do `from apex import amp`. The error will disappear if you uninstall `apex`, or fix the install if you wanted to use it.

solved!

I made a docker like here: https://github.com/boostcampaitech2/final-project-level3-nlp-09/commit/f27b7a0dcaa1f6bb0de0944731f92b2e6e841527

Hello, 

I installed transformers from the conda channel huggingface in linux. The Transformers version installed: "4.32.1". FYI, Python version: 3.9.15, Conda version: 4.10.3, Conda build version:3.21.5 . 

I tried to check whether the installation is working by running the python command given in the huggingface transformers installation page, I am getting the following error: 

Traceback (most recent call last):
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/transformers/utils/import_utils.py", line 1130, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/transformers/pipelines/__init__.py", line 60, in <module>
    from .document_question_answering import DocumentQuestionAnsweringPipeline
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/transformers/pipelines/document_question_answering.py", line 29, in <module>
    from .question_answering import select_starts_ends
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/transformers/pipelines/question_answering.py", line 9, in <module>
    from ..data import SquadExample, SquadFeatures, squad_convert_examples_to_features
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/transformers/data/__init__.py", line 26, in <module>
    from .metrics import glue_compute_metrics, xnli_compute_metrics
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/transformers/data/metrics/__init__.py", line 20, in <module>
    from sklearn.metrics import f1_score, matthews_corrcoef
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/sklearn/__init__.py", line 83, in <module>
    from .base import clone
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/sklearn/base.py", line 19, in <module>
    from .utils import _IS_32BIT
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/sklearn/utils/__init__.py", line 19, in <module>
    from . import _joblib, metadata_routing
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/sklearn/utils/_joblib.py", line 8, in <module>
    from joblib import (
ImportError: cannot import name 'Memory' from 'joblib' (unknown location)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1055, in _handle_fromlist
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/transformers/utils/import_utils.py", line 1120, in __getattr__
    module = self._get_module(self._class_to_module[name])
  File "/home/tasmia/anaconda3/envs/minigrid_dev/lib/python3.9/site-packages/transformers/utils/import_utils.py", line 1132, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback):
cannot import name 'Memory' from 'joblib' (unknown location)

Also, getting the same error for importing transformers.trainer while using image pretraining from https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-pretraining. 

Is it any issue related to my installation? how to fix this? any help would be truly appreciated. 



Hi @tasmiatasrin - it looks like the error is arising from the sklearn installation in your library. I would first try to reinstall that. Note: the package version on conda is not the most recent release available. I would advise installing with pip as you package manager to install v4.34.1.

while I tried
from transformers import Trainer

This error popped up 

ImportError: DLL load failed while importing lib: The specified procedure could not be found.

RuntimeError: Failed to import transformers.trainer because of the following error (look up to see its traceback):
DLL load failed while importing lib: The specified procedure could not be found.


How can I resolve this error?

Environment
Anaconda 
Cuda 12.3
Python  3.9.18
torch                         2.1.0+cu118
torchaudio                    2.1.0+cu118
torchvision                   0.16.0+cu118
transformers                  4.35.2


Hi @SharathHebbar as this appears to be a separate problem, could you open a new issue?