Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'module' object is not subscriptable

Tags:

python

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?

like image 840
DammondCircuit Avatar asked Mar 31 '17 18:03

DammondCircuit


People also ask

What does object not Subscriptable mean?

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.

How do you make an object Subscriptable in Python?

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__ .

How do you resolve an int object is not Subscriptable in Python?

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.

Why is set in Python not Subscriptable?

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.


1 Answers

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'])
like image 92
Aaron N. Brock Avatar answered Sep 17 '22 16:09

Aaron N. Brock