使用 IDLE 导入模块时出错
我是一个使用 IDLE (Python 2.6.4) 学习基础知识的绝对初学者。我最近发现了一个我想要运行的 Python 程序,但它抛出了一个错误,尽管代码看起来不错(即所有模块都存在):
from css.parse import parse
data = """
em {
padding: 2px;
margin: 1em;
border-width: medium;
border-style: dashed;
line-height: 2.4em;
}
p { color: red; font-size: 12pt }
p:first-letter { color: green; font-size: 200% }
p:first-line { color: blue }"""
for rule in parse(data):
print rule
for decl in parse(data)[0]:
print decl
错误:
ImportError: No module named parse
如何解决此问题?我用的是雪豹。
编辑:我猜这是一个 PATH 问题,我应该将模块放在哪里,哪个目录?
I'm an absolute beginner using IDLE (Python 2.6.4) to learn the basics. I recently found a Python program that I want to run but it throws an error although the code looks fine (i.e all modules exist):
from css.parse import parse
data = """
em {
padding: 2px;
margin: 1em;
border-width: medium;
border-style: dashed;
line-height: 2.4em;
}
p { color: red; font-size: 12pt }
p:first-letter { color: green; font-size: 200% }
p:first-line { color: blue }"""
for rule in parse(data):
print rule
for decl in parse(data)[0]:
print decl
Error:
ImportError: No module named parse
How do I fix this? I'm using Snow Leopard.
Edit: I guess its a PATH issue, where should I place the modules, which directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将
test.py
脚本放在与该文件夹相同的文件夹中,而不是在文件夹中。所以它应该看起来像这样:
You should have your
test.py
script in the same folder as the folder, not in the folder.So it should look like this:
关于 Python 模块搜索路径的方式的通用但有用的参考
Python 文档的这个简短但信息丰富的部分
允许配置解释器这一重要功能的路径和环境变量因不同的操作系统而异,因此了解此信息对于解决类似于 Nimbuz 的情况非常重要。
此外,IDE 本身还可以添加另一层配置/间接...
A generic but useful reference regarding the way Python module search path is
this brief but informative section of Python Documentation
The default paths and the environmental variables which allow configuring this important feature of the interpreter varies with different Operating Systems, so it is important to know this info for troubleshooting situations similar to that of Nimbuz.
Also the IDEs themselves can add yet another layer of configuration/indirection...