I'm using python version 3.6.
mystuff.py
includes:
mystuff = {'donut': "SHE LOVES DONUTS!"}
mystuffTest.py
includes this
import mystuff
print (mystuff['donut'])
The error that I receive when I run mystuffTest.py
is as follows:
$ python3.6 mystuffTrythis.py
Traceback (most recent call last):
File "mystuffTrythis.py", line 3, in <module>
print (mystuff['donut'])
TypeError: 'module' object is not subscriptable
So far I haven't seen this exact error here on stackoverflow. Can anyone explain why I am getting this error?
Python TypeError: 'int' object is not subscriptableThis error occurs when you try to use the integer type value as an array. In simple terms, this error occurs when your program has a variable that is treated as an array by your function, but actually, that variable is an integer.
The type of 'class object' is define by __metaclass__ , default to type . So if you need a subscriptable class, just implement __getitem__ in its __metaclass__ .
The TypeError: 'int' object is not subscriptable error occurs if we try to index or slice the integer as if it is a subscriptable object like list, dict, or string objects. The issue can be resolved by removing any indexing or slicing to access the values of the integer object.
The Python "TypeError: 'set' object is not subscriptable in Python" occurs when we try to access a set object at a specific index, e.g. my_set[0] . To solve the error, use square brackets to declare a list, because set objects are unordered and not subscriptable.
import mystuff
is importing the module mystuff
, not the variable mystuff
. To access the variable you'd need to use:
import mystuff
print(mystuff.mystuff['donut'])
EDIT: It's also possible to import the variable directly, using:
from mystuff import mystuff
print(mystuff['donut'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With