Tag Archives: Python AttributeError

How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’

AttributeError: ' dict ' object has no attribute ' item '

This error means that python cannot find the attributes of the corresponding object, and the beginners don’t know enough about the function object, which leads to errors

Original code:

favorite_languages ​​= {
     ' jen ' : ' python ' ,
     ' sarah ' : ' c ' ,
     ' edward ' : ' ruby ' ,
     ' phil ' : ' python ' ,
    }
for name,language in favorite_languages.item():
     print (name.title()+ " 's favorite language is " +language.title()+ " . " )

The text editors we use generally use obvious colors as hints:

 

 Modified code:

favorite_languages ​​= {
     ' jen ' : ' python ' ,
     ' sarah ' : ' c ' ,
     ' edward ' : ' ruby ' ,
     ' phil ' : ' python ' ,
    }
for name,language in favorite_languages.items():
     print (name.title()+ " 's favorite language is " +language.title()+ " . " )

operation result:

1 Jen ' s favorite language is Python. 
2 Sarah ' s favorite language is C. 
3 Edward ' s favorite language is Ruby. 
4 Phil ' s favorite language is Python. 
5 Sarah ' s favorite language is C.

 Summarize the commonly used Python error types as follows:

  • ZeroDivisionError-divide (or modulo) zero (all data types) 
  • ValueError-Invalid parameter passed in 
  • AssertionError-assertion statement failed 
  • StopIteration-the iterator has no more values 
  • IndexError-there is no such index in the sequence (index) 
  • IndentationError-indentation error 
  • OSError-input/output operation failed 
  • ImportError——Failed to import module/object 
  • NameError-Object not declared/initialized (no attributes) 
  • AttributeError- indicating that the object does not have this attribute
  • GeneratorExit-an exception occurs in the generator to notify the exit 
  • TypeError-invalid operation on the type 
  • KeyboardInterrupt-user interrupt execution (usually input ^C) 
  • OverflowError-Numerical operation exceeds the maximum limit 
  • FloatingPointError-floating point calculation error 
  • BaseException-the base class of all exceptions 
  • SystemExit-interpreter requests to exit 
  • Exception-the base class for general errors 
  • StandardError-the base class for all built-in standard exceptions 
  • ArithmeticError-the base class for all numerical calculation errors 
  • EOFError-there is no built-in input, reaching the EOF mark 
  • EnvironmentError-the base class for operating system errors 
  • WindowsError-system call failed 
  • LookupError-the base class for invalid data query 
  • KeyError-there is no such key in the map 
  • MemoryError-memory overflow error (not fatal to the Python interpreter) 
  • UnboundLocalError-access to uninitialized local variables 
  • ReferenceError-Weak reference attempts to access objects that have been garbage collected 
  • RuntimeError-general runtime error 
  • NotImplementedError-method not yet implemented 
  • SyntaxError Python-syntax error 
  • TabError-Tab and space are mixed 
  • SystemError-general interpreter system error 
  • UnicodeError-Unicode related errors 
  • UnicodeDecodeError-Unicode decoding error 
  • UnicodeEncodeError-Unicode encoding error 
  • UnicodeTranslateError-Unicode conversion error

The following are warning types 

  • Warning-the base class of warnings 
  • DeprecationWarning-warning about deprecated features 
  • FutureWarning-a warning that the semantics of the structure will change in the future 
  • OverflowWarning-old warning about automatic promotion to long integer (long) 
  • PendingDeprecationWarning-warning about the feature will be deprecated 
  • RuntimeWarning-warning of suspicious runtime behavior 
  • SyntaxWarning-warning of suspicious syntax 
  • UserWarning-warning generated by user code

Python AttributeError: module ‘tensorflow‘ has no attribute ‘InteractiveSession‘

Error occurred while running tensorflow:

AttributeError: module 'tensorflow' has no attribute 'InteractiveSession'

This is not the first mock exam error in the package, because the module Session has been removed in the new Tensorflow 2 version, and the code is changed to:

sess = tf.InteractiveSession()

Replace with:

sess = tf.compat.v1.InteractiveSession()

Similarly, if there are similar “TF. * *” codes in the code, you should add “compat. V1.” after them.

If you are not used to it, you can reduce the version of tensorflow

pip install tensorflow==1.14

How to Solve Python AttributeError: ‘module’ object has no attribute ‘xxx’

Python script error attributeerror: ‘module’ object has no attribute ‘xxx’ solution

when you encounter a few problems, you should pay attention to the same problem when you don’t ask for a solution

1. When naming py script, it should not be the same as the reserved word and module name of Python
(it is not easy to notice when naming files)
2 Delete the. PyC file of the library (because the. PyC file will be generated every time the PY script runs; if the. PyC file has been generated, if the code is not updated, the runtime will still go PyC, so you need to delete the. PyC file), rerun the code, or find an environment where you can run the code and copy and replace the. PyC file of the current machine Import questions </ font> </ font>

How to Solve attributeerror: ‘list’ object has no attribute ‘shape‘

Explanation:

AttributeError: ‘list’ object has no attribute ‘shape’

Property error: the ‘list’ object does not have the property ‘shape’

resolvent:

Use numpy or panda np.array Or dataframe has shape, which can be multi-dimensional, while list is one-dimensional and cannot be converted

If conversion is needed, list is converted to dataframe. Example:

a = [['a', 'b', 'c'], ['1', '2', '3'], ['张三', '张三', '张三']]
df = pd.DataFrame(a, columns=['one', 'two', 'three'])
print(df)

List to numpy example:

a = [['a', 'b', 'c'], ['1', '2', '3'], ['张三', '张三', '张三']]
data = np.array(a)
print(data)

Note: shape is a dimension. Only data frame and matrix have dimensions, while list is a slice single dimension