问题
I tried to import plotly
in Anaconda
with code
import plotly.graph_objs as go
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
but received error
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-11-978bd9a5088a> in <module>
14
15 # import plotly.plotly as py
---> 16 import plotly.graph_objs as go
17 from plotly import __version__
18 from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
ModuleNotFoundError: No module named 'plotly'
I have checked a number of similar posts to install 'plotly'
on Jupyter
but somehow I couldn't do it on Mac. I usually use Google Colab, could it be the reason that I am using different platforms that caused the confusion when installing?
回答1:
Jupyter Notebooks start in an "environment". Anaconda created the environment and it contains a lot of useful libraries. It also holds a self-contained python interpreter. In this instance, the environment didn't have the library plotly installed (it's not a default library that Anaconda provides) so you had to install plotly in your environment that the notebook lives in.
The environments that are used with Jupyter Notebooks are a little tricky to get to in order to install things, so this way, using import sys
then installing the library with !{sys.executable} -m pip install plotly
finds the python interpreter with !{sys.executable}
and installs plotly using pip right in the Notebook itself.
More reading:
- Environments
- Pip
- Packages included with Anaconda
try:
import sys
!{sys.executable} -m pip install plotly
import plotly.graph_objs as go
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
来源:https://stackoverflow.com/questions/60808369/how-to-install-plotly-from-inside-anaconda-under-colab-on-mac