Search code examples
pythonpython-3.ximportplotlypython-module

Python plotly is working in command line but not in python file - import error


plolty 4.5.4 has been installed via conda install -c plotly plotly
python version: 3.7.6

in anaconda CLI, the following lines from plotly quickstart guide display a beautiful plotly graph: enter image description here

However the same content in a .py file:

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)

And then a launch of this file:

python plotly.py

results in an import error:

Traceback (most recent call last):
File "plotly.py", line 1, in module
from plotly import graph_objs as go
File "plotly.py", line 1, in
from plotly import graph_objs as go
ImportError: cannot import name 'graph_objs' from 'plotly'

Can anyone helps ?


Solution

  • This is the well known name shadowing trap.

    Just name the file differently from any standard library, third party package, or module that the application relies on.