繁体   English   中英

用户定义的模块存储

[英]User defined modules storage

我几乎是Python的新手,并不能完全理解下面的场景。

首先,没有模块,因此下面的错误是可以理解的。

>>> import module1

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import module1
ImportError: No module named module1

然后我创建了模块,下面的句子工作得很好。

module1.py

def func():
    print 'this is a test module'

Python IDLE

>>> import module1
>>> module1.func()
this is a test module

在执行第一个语句时,创建了module1.pyc文件。 然后我向module1.py添加了另一个函数。

module1.py

def func():
    print 'this is a test module'

def func1():
    print 'this is func1'

并尝试导入此新文件并运行以下语句。 但错误被抛出如下。

Python IDLE

>>> import module1
>>> module1.func1()

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    module1.func1()
AttributeError: 'module' object has no attribute 'func1'

然后我从文件夹中删除了module1.py ,然后重新保存了module1.py (没有更改内容)。 我再次在IDLE提示符中执行了上述语句,并抛出了同样的错误。 而且这次执行import module1语句时,不会创建与前一次不同的module1.pyc文件。

但是,在重新启动IDLE时,一切都运行良好,但为什么IDE不会在每次执行import语句时重新编译module1.pyc (无需重新启动或打开另一个IDLE窗口),或者任何人都可以解释执行import语句时内存中究竟发生了什么每次。

谢谢!!

如果导入已导入的模块,则不会“重新导入”,只会获得对原始模块的引用。

如果模块已经导入,您应该使用reload来重新导入模块:

reload(module1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM