【问题标题】:Why does this script run in Idle but not in Jupyter Notebook?为什么这个脚本在 Idle 中运行而不在 Jupyter Notebook 中运行?
【发布时间】:2019-09-19 15:35:00
【问题描述】:

尝试创建一个表格来比较计算数字平方根的不同方法。使用从 1 到 9 的整数测试脚本。

附加的脚本之前在 Idle 中运行正常,但现在我在导入制表时遇到了另一个错误。该脚本从未在 Jupyter Notebook 中成功运行。

当我在 Jupyter Notebook 中运行代码时,唯一的变化是 In [] 更改为 In [*]。

总的来说,我是 Jupyter Notebook/Idle/Python 的新手。我理解 In [*] 表示内核正在运行,但我不明白为什么计算会在 Idle 中运行正常而不是在 Jupyter Notebook 中运行。

这两个程序都与 Anaconda 一起安装。 Python 版本为 3.7.3。使用的所有模块都安装在 anaconda 中。操作系统是 Windows 10。

'''Function to compare two methods of finding the square root 
of a value

'''This works in Idle but can not run in Jupyter Notebook. I don't understand why.

from tabulate import tabulate as tb
import numpy as np
import math

def mysqrt(a):

    #x = float(input('What is a reasonable estimate for ' + str(a) + '?\n'))
    #epsilon = float(input('What is your accuracy tolerance?\n'))

    x = a/2
    epsilon = 0.05

    while True:
        y = (x + a/x) / 2
        if abs(y-x) < epsilon:
            return y
            break
        x = y

def test_square_root():

    headers = ["a","mysqrt(a)", "math.sqrt(a)","diff"]
    table = np.zeros((9,4))

    for i in range(1,9):

        my_a = mysqrt(i)
        math_a = math.sqrt(i)
        diff = abs(my_a - math_a)

        print(np.shape(table[0,:]))
        table[i,:] = [i,my_a,math_a,diff]

    print(tb(table,headers))

test_square_root()

当程序之前在 Idle 中运行时,它会输出一个带有 4 列 10 行的格式化表格,包括标题。

当我现在在空闲状态下运行脚本时,我收到错误消息:

Traceback (most recent call last):
  File "C:\Users\aIDAN\Documents\Python Scripts\Think Python\test2.py", line 1, in <module>
    from tabulate import tabulate as tb
ModuleNotFoundError: No module named 'tabulate'

在 Jupyter Notebook 中,我没有收到任何输出或反馈,只有 In [] 更改为 In [*])

感谢您的宝贵时间!

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案1】:

    当前的问题是您的 python 库中没有 tabulate 模块。 它是python标准发行版中不附带的第三方包

    使用

    pip3 安装表格

    安装表格模块。

    在 Linux 中,在用户上下文中安装制表模块的命令如下所示。

    [kuvivek@vivekcentos ~]$ python3.7 -m pip install tabulate --user
    Collecting tabulate
      Using cached https://files.pythonhosted.org/packages/c2/fd/202954b3f0eb896c53b7b6f07390851b1fd2ca84aa95880d7ae4f434c4ac/tabulate-0.8.3.tar.gz
    Installing collected packages: tabulate
      Running setup.py install for tabulate ... done
    Successfully installed tabulate-0.8.3
    [kuvivek@vivekcentos polls]$ 
    

    【讨论】:

    • 感谢您的回复。不幸的是,我不能使用 pip3 命令。我打开 Anaconda Prompt 并输入 pip3 install tabulate 但收到回复:'pip3' is not recognized as an internal or external command, operable program or batch file. 我意识到我没有在原始问题中指定,但我使用的是 Windows 10。
    • 如果pip3不可用,请使用python -m pip install tabulate
    • 现在整理好了。谢谢大家!
    【解决方案2】:

    您缺少依赖项。
    要解决问题并使用命令行安装它,请激活您的 Conda 环境并编写:

    python -m pip install tabulate
    

    完成后尝试再次运行代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多