It is strange to encounter a problem recently in writing a Python script: Thepython script is perfectly fine, but the total error "attributeerror: 'module ' object has no Attribute ' xxx '. This is actually. There is a problem with the PYc file.
Problem Locator:
View The source file of the import library, discover that the source file exists and that there are no errors, and that the source file exists. PYC file
Problem Solving methods:
1. When naming the py script, do not match the python reserved word, module name, etc.
2. Delete the. pyc file for the library (because the. pyc file is generated by the PY script each time it is run; If the code is not updated, the runtime will still walk PYc when the. pyc file is generated, so delete the. pyc file), rerun the code, or find an environment where you can run the code, and copy the. pyc file to replace the
Example:
The following Python code, execution error "attributeerror: 'module ' object has no attribute ' Urlopen '", Update python27\lib\ Urllib2.pyc the file, it will run normally.
1234 |
import urllib2 url = "http://www.baidu.com" f = urllib2.urlopen(url, timeout=5).read() print len(f) |
Appendix:
PYc File Description
The PYc file, which is a python-compiled bytecode (bytecode) file. As soon as you run the py file, the Python compiler will automatically generate a corresponding PYC bytecode file. This PYC bytecode file, after the Python interpreter, generates machine code to run (which is why the PYc file can be deployed across platforms, similar to Java's cross-platform, a bytecode file run by the JVM in Java). The next call calls PYc directly, without invoking the py file. Until you have changed this py file. The Python interpreter checks the build time in the PYc file to compare the modification time of the Py file and, if the PY is updated, generates a new PYC.
Python script error Attributeerror: ' Module ' object has no attribute ' xxx ' workaround