Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: module 'typing' has no attribute '_ClassVar' #8638

Closed
1 task
HatterTheMad opened this issue Nov 18, 2020 · 13 comments
Closed
1 task

AttributeError: module 'typing' has no attribute '_ClassVar' #8638

HatterTheMad opened this issue Nov 18, 2020 · 13 comments

Comments

@HatterTheMad
Copy link

HatterTheMad commented Nov 18, 2020

Environment info

  • transformers version: 3.5.1
  • Platform: Linux
  • Python version: 3.8
  • PyTorch version (GPU?): 1.7.0+cpu
  • Using GPU in script?: No
  • Using distributed or parallel set-up in script?: Yes

Who can help @sgugger .... @LysandreJik ... mb?

Information

Model I am using: Distilbert

The problem arises when using:
Just this:

from transformers import AutoTokenizer, AutoModelForQuestionAnswering

The tasks I am working on is:

  • my own task or dataset:
    I am using default Distilbert for my flask API

To reproduce

Steps to reproduce the behavior:
That's a big part of the question. It works just fine on my local machine, but gives this error when run on my AWS server.

from flask import Flask, request, jsonify
from flask import Flask
from flask_restful import Api, Resource
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
from transformers.pipelines import pipeline

tokenizer = AutoTokenizer.from_pretrained('./Dis_Save/')
model = AutoModelForQuestionAnswering.from_pretrained('./Dis_Save/')
nlp_qa = pipeline('question-answering', tokenizer=tokenizer,model=model)

app = Flask(__name__)

@app.route('/api/QandA', methods=['GET', 'POST'])

def QandA():
    content = request.json
    print(content['userMessages'])
    X = nlp_qa(context=content['userMessages'], question=content['question'])
    return(jsonify({"answer":X["answer"], "score":X["score"]}))

if __name__ == "__main__":
	app.run(debug=True)

#This is all the code that I have. Here is the full error that I get:

  File "access/main.py", line 4, in <module>
    from transformers import AutoTokenizer, AutoModelForQuestionAnswering
  File "/home/ubuntu/access/transformers/__init__.py", line 22, in <module>
    from .integrations import (  # isort:skip
  File "/home/ubuntu/access/transformers/integrations.py", line 82, in <module>
    from .trainer_callback import TrainerCallback  # noqa: E402
  File "/home/ubuntu/access/transformers/trainer_callback.py", line 27, in <module>
    from .training_args import TrainingArguments
  File "/home/ubuntu/access/transformers/training_args.py", line 36, in <module>
    class TrainingArguments:
  File "/home/ubuntu/access/dataclasses.py", line 958, in dataclass
    return wrap(_cls)
  File "/home/ubuntu/access/dataclasses.py", line 950, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
  File "/home/ubuntu/access/dataclasses.py", line 800, in _process_class
    cls_fields = [_get_field(cls, name, type)
  File "/home/ubuntu/access/dataclasses.py", line 800, in <listcomp>
    cls_fields = [_get_field(cls, name, type)
  File "/home/ubuntu/access/dataclasses.py", line 659, in _get_field
    if (_is_classvar(a_type, typing)
  File "/home/ubuntu/access/dataclasses.py", line 550, in _is_classvar
    return type(a_type) is typing._ClassVar
AttributeError: module 'typing' has no attribute '_ClassVar'
@LysandreJik
Copy link
Member

This is weird! Looking at the stacktrace, I have a few questions:

  • Are you running a file in access/main.py?
  • If yes, is there a dataclasses.py file in that access folder?

It seems far-fetched but if that's the case, it might be possible that this file is interfering with the dataclasses module.

@attardi
Copy link

attardi commented Nov 19, 2020

It seems related to an incompatibility of python > 3.6 with package dataclasses, as explained here:

    https://github.com/google/flax/pull/270

@LysandreJik
Copy link
Member

Yes, but we only install dataclasses on Python versions that are inferior to 3.7: https://github.com/huggingface/transformers/blob/master/setup.py#L137

@nsaef
Copy link

nsaef commented Nov 25, 2020

I encountered the same problem with the following setup:

  • transformers version: 3.5.1
  • Platform: Linux
  • Python version: 3.8
  • PyTorch version (GPU?): 1.7.0+cpu
  • Using GPU in script?: No
  • Using distributed or parallel set-up in script?: No

Local execution works fine, but when running the code on Google App Engine (Standard Environment), it fails with error AttributeError: module 'typing' has no attribute '_ClassVar'. There is no file called dataclasses.py anywhere in the project,

Stacktrace:

File "/srv/application/stance_pred/bert_inference.py", line 8, in <module>      from transformers import DistilBertForSequenceClassification, DistilBertTokenizer, DistilBertConfig
File "/layers/google.python.pip/pip/transformers/__init__.py", line 22, in <module>      from .integrations import (  # isort:skip
File "/layers/google.python.pip/pip/transformers/integrations.py", line 82, in <module>      from .trainer_callback import TrainerCallback  # noqa: E402
File "/layers/google.python.pip/pip/transformers/trainer_callback.py", line 27, in <module>      from .training_args import TrainingArguments
File "/layers/google.python.pip/pip/transformers/training_args.py", line 36, in <module>      class TrainingArguments:
File "/layers/google.python.pip/pip/dataclasses.py", line 958, in dataclass      return wrap(_cls)
File "/layers/google.python.pip/pip/dataclasses.py", line 950, in wrap      return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
File "/layers/google.python.pip/pip/dataclasses.py", line 800, in _process_class      cls_fields = [_get_field(cls, name, type)
File "/layers/google.python.pip/pip/dataclasses.py", line 800, in <listcomp>      cls_fields = [_get_field(cls, name, type)
File "/layers/google.python.pip/pip/dataclasses.py", line 659, in _get_field      if (_is_classvar(a_type, typing)
File "/layers/google.python.pip/pip/dataclasses.py", line 550, in _is_classvar      return type(a_type) is typing._ClassVar  AttributeError: module 'typing' has no attribute '_ClassVar'

@thesby
Copy link

thesby commented Nov 27, 2020

Any solution?

@LysandreJik
Copy link
Member

Could one of you post the result of pip list in the environment where that is failing? Or even better paste the result of pip freeze, alongside a few lines of code that reproduce the issue.

Thank you!

@thesby
Copy link

thesby commented Nov 30, 2020

I solved this problem by removing dataclasses*

@HatterTheMad
Copy link
Author

I have solved this problem by downgrading to Python version: 3.6

Thanks @attardi

@stas00
Copy link
Contributor

stas00 commented Mar 4, 2021

note that fairseq 0.10.1 requires dataclasses even for py>3.7 where it's built-in and HF Trainer breaks when dataclasses are installed for these versions. So if some project pulls in fairseq which will force the install of dataclasses HF Trainer will break. Probably need to ask fairseq to fix their dependencies.

Until then @thesby's solution is the easiest one.

pip uninstall dataclasses -y

@vicesodan
Copy link

I don't know why, but for me changing the version of Python worked (from 3.9.12 to 3.9.5.)

@loretoparisi
Copy link
Contributor

note that fairseq 0.10.1 requires dataclasses even for py>3.7 where it's built-in and HF Trainer breaks when dataclasses are installed for these versions. So if some project pulls in fairseq which will force the install of dataclasses HF Trainer will break. Probably need to ask fairseq to fix their dependencies.

Until then @thesby's solution is the easiest one.

pip uninstall dataclasses -y

In my case pip command itself was broken so I had to do by hand:

rm -rf lib/dataclasses-0.6.dist-info
rm lib/dataclasses.py

in the location where dataclasses has been installed!

@wookayin
Copy link

wookayin commented Nov 10, 2022

Dependency to dataclasses in setup.py should be python 3.6 only:

-'dataclasses'
+'dataclasses; python_version < "3.7"'

or use if sys.version_info >= (3, 7): check to dynamically configure _deps in https://github.com/huggingface/transformers/blob/main/setup.py#L98.

@benj3542
Copy link

I met the same error without having dataclasses installed in my environment (python 3.9). I installed dataclasses and uninstalled it again - from this point on the error disappeared.

FilipeMarch added a commit to FilipeMarch/python-for-android that referenced this issue Feb 1, 2023
There is [incompatibility](huggingface/transformers#8638) between python > 3.6 and package `dataclasses`.

This error was happening after my compilations: `AttributeError: module 'typing' has no attribute '_ClassVar'`

The fix is removing `dataclasses` from `python_depends`.

I also updated the pydantic version to the newest release `1.10.4`, it can be checked [here](https://github.com/pydantic/pydantic/releases) and its url.

I already compiled my app after applying these changes, and the compilation is working successfully again.
misl6 added a commit to kivy/python-for-android that referenced this issue Feb 11, 2023
* Update `pydantic` recipe

There is [incompatibility](huggingface/transformers#8638) between python > 3.6 and package `dataclasses`.

This error was happening after my compilations: `AttributeError: module 'typing' has no attribute '_ClassVar'`

The fix is removing `dataclasses` from `python_depends`.

I also updated the pydantic version to the newest release `1.10.4`, it can be checked [here](https://github.com/pydantic/pydantic/releases) and its url.

I already compiled my app after applying these changes, and the compilation is working successfully again.

* Restrict sh version to sh>=1.10, <2.0 (#2746)

keep sh version under 2.0 as default behaviour was switched

* Update CHANGELOG.md and update version to 2023.02.10

---------

Co-authored-by: Filipe Marchesini <filipe.marchesini@gmail.com>
Co-authored-by: Andre Miras <AndreMiras@users.noreply.github.com>
Co-authored-by: HyTurtle <81598434+HyTurtle@users.noreply.github.com>
shyamnathp pushed a commit to shyamnathp/python-for-android that referenced this issue Feb 17, 2023
There is [incompatibility](huggingface/transformers#8638) between python > 3.6 and package `dataclasses`.

This error was happening after my compilations: `AttributeError: module 'typing' has no attribute '_ClassVar'`

The fix is removing `dataclasses` from `python_depends`.

I also updated the pydantic version to the newest release `1.10.4`, it can be checked [here](https://github.com/pydantic/pydantic/releases) and its url.

I already compiled my app after applying these changes, and the compilation is working successfully again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants