Python reload (module) usage example details, pythonreload

Source: Internet
Author: User

Python reload (module) usage example details, pythonreload

Preface

This article mainly introduces the reload (module) usage in python and shares it for your reference. I will not talk about it much below. Let's take a look at the detailed introduction.

1. Differences in reload () usage between Python2 and Python3.

In Python2, you can directly use reload (module) to overload the module.

Use the following method in Pyhton3:

(1)

>>> from imp >>> imp.reload(module)

(2)

>>> from imp import reload >>> reload(module)

2. Cause of error when using import and reload () in Python3

Assume that recommendations. py is stored in the C: \ Python34 \ PCI_Code \ chapter2 \ directory, which contains the critics function.

If the following error occurs during the import function,

>>> from recommendation import critics Traceback (most recent call last):  File "<pyshell#7>", line 1, in <module>  from recommendation import critics ImportError: No module named 'recommendation' 

Add the directory C: \ Python34 \ PCI_Code \ chapter2 \ To the system path.

>>> import sys >>> sys.path.append("C:\Python34\PCI_Code\chapter2") 
>>> from recommendations import critics >>> 

Usereload()The following error occurs:

>>> from imp import reload >>> reload(recommendations) Traceback (most recent call last):  File "<pyshell#86>", line 1, in <module>  reload(recommendations) NameError: name 'recommendations' is not defined 

The reason is thatimport reloadAfter that, you need to load the module in the import. At this time, reload will not be a problem. For details, refer to the following code:

>>> from imp import reload >>> import recommendations >>> reload(recommendations) <module 'recommendations' from 'C:\\Python34\\PCI_Code\\chapter2\\recommendations.py'> >>> 

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.