【问题标题】:Python plotly is working in command line but not in python file - import errorPython plotly 在命令行中工作,但不在 python 文件中 - 导入错误
【发布时间】:2020-07-05 03:28:51
【问题描述】:

plolty 4.5.4 已通过 conda install -c plotly plotly 安装
python版本:3.7.6

在 anaconda CLI 中,plotly 快速入门指南中的以下几行显示了一个漂亮的 plotly 图表:

但是 .py 文件中的内容相同:

import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
fig.write_html('first_figure.html', auto_open=True)

然后启动这个文件:

python plotly.py

导致导入错误:

回溯(最近一次通话最后一次):
模块中的文件“plotly.py”,第 1 行
从 plotly import graph_objs as go
文件“plotly.py”,第 1 行,在
从 plotly import graph_objs as go
ImportError:无法从“plotly”导入名称“graph_objs”

谁能帮忙?

【问题讨论】:

  • 不要将您的文件命名为 plotly.py,这会掩盖 plotly 模块的名称

标签: python python-3.x import plotly python-module


【解决方案1】:

这是众所周知的name shadowing trap

只需将文件命名为与应用程序所依赖的任何标准库、第三方包或模块不同的名称即可。

【讨论】:

  • 谢谢...最糟糕的是,这不是我第一次陷入这个陷阱...
猜你喜欢
  • 1970-01-01
  • 2017-03-12
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 2023-01-20
  • 2018-02-22
相关资源
最近更新 更多