先来看个示例,目录结构如下:
base.py文件代码:#!/usr/bin/python
class Base:
def call(self):
print 123
test.py文件代码:#!/usr/bin/python
import base
a = Base()
a.call()
执行test.py,输出结果:Traceback (most recent call last):
File "C:UsersdondonliuDesktoptesttest.py", line 5, in
a = Base()
NameError: name 'Base' is not defined
执行出错了。修改test.py文件代码:#!/usr/bin/python
import base
a = base()
a.call()
执行,输出结果:Traceback (most recent call last):
File "C:UsersdondonliuDesktoptesttest.py", line 5, in
a = base()
TypeError: 'module' object is not callable
还是报错了。修改test.py代码:#!/usr/bin/python
from base import Base
a = Base()
a.call()
执行,输出结果:123
执行成功了,这是为什么呢?
查了一番资料,__使用import方式引入模块的话,使用时需要加上模块名__。
类似上面的示例,test.py正确的代码应该是:#!/usr/bin/python
import base
a = base.Base()
a.call()
本文发布于:2024-01-28 04:12:14,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063863414677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |