无法从命令行运行的 Python 代码

发布于 2024-09-25 04:06:04 字数 1093 浏览 2 评论 0原文

我在 Python 2.6 中编写的模块(test.py)可以通过 Python IDLE 使用以下命令导入并完美运行:

import test
test.run_test_suite()

但是,如果我在命令行中使用命令“python test.py”,它显然会崩溃(根据回溯)命令“import os”。

正如您从下面的代码中看到的那样,当从命令行运行时,它应该与在 IDLE 中运行时执行相同的操作。为什么在 IDLE 中运行它不会导致问题?我的 google-foo 仅当代码在命令行运行但不能在 IDLE 中运行时才能得到结果。

if __name__ == "__main__":
    table = run_test_suite()
    print '---=== Results ===---'
    print_table(table)

应该指出的是,这个模块只是做大量的基础数学来检查一些外部计算的数据是否可行。

完整的回溯是:

Traceback (most recent call last):
  File "...\Python\test.py", line 170, in <module>
    print '---=== Results ===---'
  File "...\Python\test.py", line 160, in build_data
    if Links == False:
  File "...\Python\test.py", line 103, in load_table
    if Abbrev[M.solution_type()] == 'pos':
  File "...\Python\test.py", line 85, in build_example
    import os
  File "SnapPy.pyx", line 173, in snappy.SnapPy.uFatalError (SnapPy.c:5507)
snappy.SnapPy.SnapPeaFatalError: 
SnapPea crashed in function cusp_modulus(), defined in cusp_modulus.c.

A module that I have written (test.py) in Python 2.6 can be imported and run perfectly well from with the Python IDLE with the commands:

import test
test.run_test_suite()

However if I use the command "python test.py" at the command line, it crashes apparently (according to traceback) on the command "import os".

As you can see from the code below, when run from the command line it should perform the same as when run inside the IDLE. Why would not running this in the IDLE cause a problem? My google-foo only can up with results when code would run at the command line but not in IDLE.

if __name__ == "__main__":
    table = run_test_suite()
    print '---=== Results ===---'
    print_table(table)

It should be pointed out that this module is doing nothing more than large amounts of basic maths to check some externally calculated data is feasible.

the full traceback is:

Traceback (most recent call last):
  File "...\Python\test.py", line 170, in <module>
    print '---=== Results ===---'
  File "...\Python\test.py", line 160, in build_data
    if Links == False:
  File "...\Python\test.py", line 103, in load_table
    if Abbrev[M.solution_type()] == 'pos':
  File "...\Python\test.py", line 85, in build_example
    import os
  File "SnapPy.pyx", line 173, in snappy.SnapPy.uFatalError (SnapPy.c:5507)
snappy.SnapPy.SnapPeaFatalError: 
SnapPea crashed in function cusp_modulus(), defined in cusp_modulus.c.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

薄荷梦 2024-10-02 04:06:04

您在这两种情况下使用相同的 python 版本吗?从命令行启动时,您将获得路径中的第一个 Python,而 IDLE 很可能直接从快捷方式执行。
如果您的计算机上安装了多个版本的 python,则这可能会在两个完全不同的环境中进行转换。

Are you using the same python version in both cases? When starting from the commandline, you get the first Python in your path, while IDLE is most probably executed directly from a shortcut.
If you have more than one version of python installed on your machine, this could translate in two complete different environments.

没有心的人 2024-10-02 04:06:04

在当前工作目录中查找名为 os.py 的文件。如果有的话,请重命名它。或者查看 Python 文档中的“绝对导入”。

Look for a file named os.py in your current working directory. If you have one, rename it. Or check the Python documentation for "absolute import".

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