使用 IDLE 导入模块时出错

发布于 2024-08-12 11:31:07 字数 719 浏览 3 评论 0原文

我是一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-08-19 11:31:07

您应该将 test.py 脚本放在与该文件夹相同的文件夹中,而不是文件夹中。

所以它应该看起来像这样:

../
 test.py
 css/

You should have your test.py script in the same folder as the folder, not in the folder.

So it should look like this:

../
 test.py
 css/
街道布景 2024-08-19 11:31:07

关于 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文