今天在学习 Python 的时候遇到这个问题,但是在百度上没有查到
from string import Template
s = Template('$x,nihao $x')
s.substitute(x='hello')
今天在学习 Python 的时候遇到这个问题,但是在百度上没有查到
from string import Template
s = Template('$x,nihao $x')
s.substitute(x='hello')
4 回答4.5k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
4 回答3.8k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
1 回答4.4k 阅读✓ 已解决
1 回答3.9k 阅读✓ 已解决
1 回答2.8k 阅读✓ 已解决
python ImportError: cannot import name *
这个错误是找到了模块,但是,模块内并没有定义要import的类或对象或函数
而python doc中 可以查到 class string.Template(template) 。
表明,string 模块中是有Template这个类的。
错误提示找不到,但实际上应该有。表明了什么,表明了并没有import到python自带的string 模块或者string 模块被破坏。
模块被破坏的概率太小了,所以最大可能是没有import 到 python自带的string 模块。
有模块用了string这个名字,但又不是python自带的。
按正常来说,一般没有第三方模块会用string这个模块名。
所以,你最好检查一下,出错的python脚本的同目录是不是有个string.py