Examples of reload usage in python

巴扎黑
Release: 2017-09-16 10:06:11
Original
2252 people have browsed it

与from和import相比,reload是内置函数,而不是语句,下面这篇文章主要给大家介绍了关于python中reload(module)用法的相关资料,文中给出了详细的示例代码供大家参考学习,需要的朋友们下面来一起看看吧。

前言

本文主要给大家介绍了关于python中reload(module)用法的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

1、Python2中可以和Python3中关于reload()用法的区别。

Python2 中可以直接使用reload(module)重载模块。

Pyhton3中需要使用如下方式:

(1)


>>> from imp 
>>> imp.reload(module)
Copy after login

(2)


>>> from imp import reload 
>>> reload(module)
Copy after login

2、Python3中使用import和reload()出现错误的原因

假设recommendations.py 放在C:\Python34\PCI_Code\chapter2\目录下,其中包含函数critics

如果在import函数的时候出现如下错误,


>>> from recommendation import critics 
Traceback (most recent call last): 
 File "<pyshell#7>", line 1, in <module> 
 from recommendation import critics 
ImportError: No module named &#39;recommendation&#39;
Copy after login

请把目录C:\Python34\PCI_Code\chapter2\加到系统路径中


>>> import sys 
>>> sys.path.append("C:\Python34\PCI_Code\chapter2")
Copy after login


>>> from recommendations import critics 
>>>
Copy after login

使用reload()时出现如下错误


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

原因是因为在import reload之后需要在import 需要加载的模块,这时候再去reload就不会有问题,具体看下面代码:


>>> from imp import reload 
>>> import recommendations 
>>> reload(recommendations) 
<module &#39;recommendations&#39; from &#39;C:\\Python34\\PCI_Code\\chapter2\\recommendations.py&#39;> 
>>>
Copy after login

The above is the detailed content of Examples of reload usage in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!