Search code examples
pythonplotlyimporterrorplotly-python

ImportError: The _plotly_future_ module must be imported before the plotly module


I try to reproduce example of making plotly plot with multiple subplots in Python which is posted as 1st example here: "Subplots in Python". I work in Jupyter notebook, plotly version: 3.10.0, Python version: 3.6.3.

After running that example:

from plotly.subplots import make_subplots
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

## Allow to generate plotly plots inline in notebook
init_notebook_mode(connected=True)

fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]), row=1, col=2)
fig.update_layout(height=600, width=800, title_text="Subplots")
iplot(fig)

I got ValueError:

ValueError: 
plotly.subplots.make_subplots may only be used in the
v4_subplots _plotly_future_ mode.  To try it out, run
>>> from _plotly_future_ import v4_subplots
before importing plotly.

I then added from _plotly_future_ import v4_subplots at the very top of the imports. I cleaned output, used %restet to clean all variables and even refreshed the notebook, but the code with the headlines:

from _plotly_future_ import v4_subplots 
from plotly.subplots import make_subplots 
import plotly.graph_objs as go from plotly.offline import download_plotlyjs, 
init_notebook_mode, plot, iplot
# (...)

results now in ImportError:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-14-d2ad52c99588> in <module>
----> 1 from _plotly_future_ import v4_subplots
      2 from plotly.subplots import make_subplots
      3 import plotly.graph_objs as go
      4 from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
      5 

c:\users\karasma6\appdata\local\continuum\anaconda3\envs\actigrapy\lib\site-packages\_plotly_future_\v4_subplots.py in <module>
      2 from _plotly_future_ import _future_flags, _assert_plotly_not_imported
      3 
----> 4 _assert_plotly_not_imported()
      5 _future_flags.add('v4_subplots')

c:\users\karasma6\appdata\local\continuum\anaconda3\envs\actigrapy\lib\site-packages\_plotly_future_\__init__.py in _assert_plotly_not_imported()
      9     if 'plotly' in sys.modules:
     10         raise ImportError("""\
---> 11 The _plotly_future_ module must be imported before the plotly module""")
     12 
     13 

ImportError: The _plotly_future_ module must be imported before the plotly module

How to fix it?


Solution

  • You may have to restart your kernel: just refreshing your notebook isn’t enough.

    That said, version 4 is out now so your best bet would be to upgrade :)