zoukankan      html  css  js  c++  java
  • python中reload(module)的用法,以及错误提示

        1、Python2中可以和Python3中关于reload()用法的区别。
    Python2 中可以直接使用reload(module)重载模块。
     
    Pyhton3中需要使用如下两种方式:
    方式(1)
    >>> from imp
    >>> imp.reload(module)
    方式(2)
    >>> from imp import reload
    >>> reload(module)
        2、Python中使用import和reload()出现错误的原因
    在Python中,以py为扩展名保存的文件就可以认为是一个模块,模块包含了 Python 对象定义和Python语句。
    假设recommendations.py 放在C:Python34PCI_Codechapter2目录下,其中包含函数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 'recommendation'
    请把目录C:Python34PCI_Codechapter2加到系统路径中,
    >>> import sys
    >>> sys.path.append("C:Python34PCI_Codechapter2")
    >>> from recommendations import critics
    >>> 
    
    或者切换到文件所在的目录中,
     
    C:Python34PCI_Codechapter2>python
    >>> from recommendations import *
    >>> 
    
    ------------------------------------------------------------------------------------------------------------------------------------
    使用reload()时出现如下错误
    >>> 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
    原因是因为在reload某个模块的时候,需要先import来加载需要的模块,这时候再去reload就不会有问题,具体看下面代码:
    >>> from imp import reload
    >>> import recommendations
    >>> reload(recommendations)
    <module 'recommendations' from 'C:\Python34\PCI_Code\chapter2\recommendations.py'>
    >>> 

    出处:http://www.itwendao.com/article/detail/93294.html

    ==================================================================================

    Python模块详细说明:> http://www.runoob.com/python/python-modules.html

  • 相关阅读:
    三次请求(读-改-读)引出nibernate 一级缓存
    算法竞赛入门经典第一、二章摘记
    uva 10905 Children's Game
    uva 11205 The broken pedometer
    uva 10160 Servicing stations
    uva 208 Firetruck
    uva 167 The Sultan's Successors
    zoj 1016 Parencodings
    uva 307 Sticks
    uva 216 Getting in Line
  • 原文地址:https://www.cnblogs.com/mq0036/p/7200052.html
Copyright © 2011-2022 走看看