Python script error Attributeerror: ' Module ' object has no attribute ' xxx ' workaround

Source: Internet
Author: User
Tags python script

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 library. pyc file (because Each time the py script is run, the. pyc file is generated; If the code is not updated, the runtime will still go PYC if 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 that replaces the current machine.

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 urllib2url = "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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.