40
>>> import attr
>>> @attr.s
... class SmartClass(object):
...     a=attr.ib()
...     b=attr.ib()
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'attr' has no attribute 's'
>>> 

I don't understand why it's not working. I have installed this module using pip and it was installed properly, but still it shows an error in the implementation part.

arctic_Oak
  • 974
  • 2
  • 13
  • 29

2 Answers2

118

The attr library on PyPI does not have an API that looks like this. There's no attr.ib or attr.s in it, and so no reason you should expect it to work.

You're probably using documentation for the attrs library, which—somewhat confusingly—installs packages named both attrs and attr, and does in fact have an attr.s and an attr.ib, used exactly the way you're attempting.

But if you want to use the attrs library, you have to install it—and you probably want to uninstall attr too:

$ pip uninstall attr
$ pip install attrs

(If you actually want both, it's a bit confusing; the recommended way seems to be to use attr from attrs, and dry_attr from attr.)

Of course the best solution is to hop in a time machine to a few months in the future so you can use Python 3.7, with its PEP 557 data classes, a feature based largely on attrs, but simpler and built in to the stdlib.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 7
    I'm from future, from 2021, I have Python 3.7 installed, but it doesn't seem to have `attr` built-in, but having to run: `pip uninstall attr` then `pip uninstall attrs` then `pip install attrs`, - seems to resolve the issue. Thx – Jerry Green Jan 10 '21 at 09:36
  • The solution by Jerry Green worked for me in June 2022. – Emil Jun 14 '22 at 11:48
  • 1
    In 2023 on Python 3.10.8, this does not work for me. I have `attrs` installed already and don't have `attr` installed, but still get the same error while debugging in PyCharm – gt6989b Mar 02 '23 at 20:38
  • 1
    I'm from future using conda, from 2023. I have Python 3.10.6 installed, but it doesn't seem to have `attr` built-in, but having to run: `pip uninstall attr attrs` then `conda install attrs --force-reinstall --update-deps`, seems to do the trick. Thx – Olivier D'Ancona Apr 03 '23 at 07:19
-6
$ python3 -c 'import attr; print(attr.__file__)'
/path/to/python3.X/site-packages/attr.py

Removing this file worked for me. So

rm $(python3 -c 'import attr; print(attr.__file__)')

solves the issue.

  • 1
    With no other information whatsoever this isn't helpful, but it did solve my issue with my jupyterhub server being able to start! – bendl Jun 09 '21 at 19:23