【问题标题】:Unable to import 'progressbar2' library无法导入“progressbar2”库
【发布时间】:2019-07-11 03:44:59
【问题描述】:

我是 python 的新手,我试图在我的代码中使用 python 库progressbar2

我尝试使用conda 命令安装该库以确保它已安装在 anaconda 环境中,并且我使用的是 VS 代码并且它也设置在相同的环境中,所以当我尝试在我的代码中导入此库时与:

from progressbar2 import *
mselection = float(input("Method number : "))
while not 1 <= mselection <= 2:
  print("Invailed value")
  mselection = float(input("Method number : "))
else:
  if mselection == 1:

    area = float(input("Area: "))
    xyratio = float(input("X/Y Ratio: "))
    y = (area/xyratio)**(1/2)
    x = (area*xyratio)**(1/2)
    values = {'x':x,'y':y}
    values['o_data'] = [area, xyratio]
    print("The hight and weidth needed to create:-\nA rectangle with area {0[o_data][0]:^10}\nThe ratio between them {0[o_data][1]:^10}\nIs({0[x]:^10},{0[y]:^10})\nFor X= higth and Y= weidth.".format(values))
    input()

  elif mselection == 2:

    def result(y=0, i=0, ratio=1):
      return print(f"Y= {y:<20} X= {i:<20} Ratio= {xyratio}")

    area = float(input("Area: "))
    xmin = float(input("Xmin: "))
    xmax = float(input("Xmax: "))
    step = float(input("Step: "))
    include = str(input("Include: "))
    i = xmin
    pbar = p
    widgets = ['Test: ', Percentage(), ' ', Bar(marker='0',left='[',right=']'),
           ' ', ETA(), ' ', FileTransferSpeed()] #see docs for other options
    pbar = ProgressBar(widgets=widgets, maxval=((xmax-xmin)*(1/step)))
    pbar.start()
    while i <= xmax:
      y = area/i
      xyratio = str(i/y)
      if include in xyratio:
            result(y=y,i=i,ratio=xyratio)
      i = i + step
      pbar.update()
      if i == 0:
        i = 1 
  pbar.finish()
  input()            

我得到这个错误:-

{"resource": "/C:/Users/User/Desktop/Rectangle higth and weidth calculator.py",
    "owner": "python",
    "code": "import-error",
    "severity": 8,
    "message": "Unable to import 'progressbar2'",
    "source": "pylint",
    "startLineNumber": 2,
    "startColumn": 1,
    "endLineNumber": 2,
    "endColumn": 1
}

一开始我遇到的问题是我还没有安装库,所以当我安装它并期望一切正常时,我得到了新的错误。

我正在使用 python 3.7.0。

【问题讨论】:

  • 什么新错误?始终将完整的错误消息(从单词“Traceback”开始)放在问题中(作为文本,而不是屏幕截图)。还有其他有用的信息。
  • 我添加了完整的错误
  • 你可能安装了两个 python。一个安装了模块,第二个运行您的代码。
  • Anaconda 为我安装了所有东西,所有库都在它的环境文件夹中,我没有手动安装任何东西
  • 我删除了那行,它工作得很好!非常感谢

标签: python python-3.x progress-bar


【解决方案1】:

此模块的名称为 progressbar2,编号为 2,您可以使用它来安装它

pip install progressbar2

但在代码中它使用没有2的名称

import time
import progressbar

for i in progressbar.progressbar(range(100)):
     time.sleep(0.02)

来自documentation的示例

【讨论】:

    【解决方案2】:

    如果你尝试过

    conda 安装进度条2

    然后进行 pip 安装

    pip 安装进度条2

    [注意:如果您没有安装“pip”,请通过此链接安装 pip https://www.youtube.com/watch?v=AVCcFyYynQY]

    [如果它不起作用,请提供更详细的错误消息。]

    【讨论】:

    • 我用 pip 安装了它,仍然出现同样的错误,我怎样才能得到更详细的错误信息?
    • easy_install progressbar2 我已经浏览了progressbar2 文档,他们建议通过pip 或easy install 安装
    • 我刚刚尝试了easy_install方法,但错误仍然存​​在
    【解决方案3】:

    使用 在linux中

    $ sudo pip install progressbar2
    

    在 windows 中以管理员身份运行 cmd 和

    pip install progressbar2
    

    【讨论】:

    • 我已经这样做了,但仍然出现这个错误,问题是在第一个VS代码中说它找不到库,我安装它之后它又说它不能导入库
    • 尝试使用大写的“P”作为库名
    猜你喜欢
    • 1970-01-01
    • 2018-04-29
    • 2018-03-26
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多