How to install 'plotly' from inside Anaconda under Colab on Mac - python

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?

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)

Related

Module plotly.offline not found

I get this error importing in a Python virtual environment,
import plotly.offline
ModuleNotFoundError: No module named 'plotly.offline'
I installed plotly using pip install plotly. Tried to uninstall and reinstall. But still same eror. I'm using Python3 in the venv. I can import plotly with import plotly but none of its modules I can't.
But when I tried a global install (not using a venv) I am able to import plotly.offline.

How to resolve import cufflinks error "The plotly.plotly module is deprecated"?

On importing cufflinks I am getting error in Jupyter Notebook:
import cufflinks as cf
error:
The plotly.plotly module is deprecated,
please install the chart-studio package and use the
chart_studio.plotly module instead.
What to do? Is there any other way I can plot my data frames using plotly?
This is because you have to use the chart_studio.plotly module instead.
Install chart-studio package pip install chart_studio
Use the chart_studio.plotly module instead of plotly.plotly
Replace Code: import plotly.plotly as py
By: import chart_studio.plotly as py

Getting plotly.graph_objs attribute error for FigureWidget

I get the following error in Jupyter Notebook:
AttributeError: module 'plotly.graph_objs' has no attribute 'FigureWidget'
I have verified that Plotly package is installed as is ipywidgets, Flask and Dash.
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
from plotly import figure_factory as FF
init_notebook_mode(connected=True)
Not sure what I need to install or update to get this to work.
For me even when plotly was upgraded ipywidget wasn't.
executing pip3 install ipywidgets --upgrade
make sure you enable these extensions on this notebook:
jupyter nbextension enable --py widgetsnbextension --sys-prefix
jupyter nbextension enable --py plotlywidget --sys-prefix

Python 3 - ModuleNotFoundError: No module named 'xgboost'

I am a total newbie in Python 3 and programming in general so I looked at other peoples code and just for the beginning paste one example into Jupyter. But right at the beginning, I get an Error "ModuleNotFoundError: No module named 'xgboost'" Why does this not work?
import pandas as pd
import numpy as np
import re
import sklearn
import xgboost as xgb // error
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.tools as tls
import warnings
warnings.filterwarnings('ignore')
# Going to use these 5 base models for the stacking
from sklearn.ensemble import (RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier, ExtraTreesClassifier)
from sklearn.svm import SVC
from sklearn.cross_validation import KFold
I am assuming you are running Anaconda, because this is the first error you encountered. You need to install this package: https://anaconda.org/anaconda/py-xgboost because the code you copied uses it and needs it.
You will probably get a plotly error too, so install https://anaconda.org/plotly/plotly and remember to restart Jupyter (or the kernel at least).
If you are not running Anaconda, run pip install xgboost and pip install plotly.
I tried
pip install xgboost
and
pip3 install xgboost
But it doesn't work
##ModuleNotFoundError: No module named 'xgboost'
It worked in the Jupyter Notebook cell
import sys
!{sys.executable} -m pip install xgboost
Go to command prompt >> By typing "cmd" in your windows search engine.>> Please type "pip install xgboost".
Later, close your Jupyter notebook and open it again. Run the respective cell.
If you are still getting the error then :
Add a cell in Jupyter notebook and type "pip install xgboost". Run this cell. Now it will work.
Giving a very detailed answer since beginners might be here too. Hope this helps! Be motivated! You can do it!
conda install -c conda-forge xgboost

Jupyter gets Import Error when importing matplotlib

I'm just now trying to integrate to Jupyter and I have been using Anaconda for a long while now.
When trying to import the matplotlib in the IPython Notebook,
import matplotlib.pyplot as plt
%matplotlib inline
I get this error:
ImportError Traceback (most recent call last)
<ipython-input-2-385145dcc870> in <module>()
----> 1 import matplotlib.pyplot as plt
2 get_ipython().magic(u'matplotlib inline')
ImportError: No module named matplotlib.pyplot
I faced the same problem. Then I used folllowing command to install matplotlib library to my virtual environment.
pip install matplotlib
Then I could import matplotlib to jupyter notebook.
I think you should install matplotlib library if using conda then in terminal type:
# create a new environment with the required packages
conda create -n "matplotlib_build" python=3.5 numpy python-dateutil pyparsing pytz tornado cycler tk libpng zlib freetype
activate matplotlib_build
# if you want a qt backend, you also have to install pyqt (be aware that pyqt doesn't mix well if
# you have created the environment with conda-forge already activated...)
conda install pyqt
# this package is only available in the conda-forge channel
conda install -c conda-forge msinttypes
# copy the libs which have "wrong" names
set LIBRARY_LIB=%CONDA_PREFIX%\Library\lib
mkdir lib || cmd /c "exit /b 0"
copy %LIBRARY_LIB%\zlibstatic.lib lib\z.lib
copy %LIBRARY_LIB%\libpng_static.lib lib\png.lib
# Make the header files and the rest of the static libs available during the build
# CONDA_DEFAULT_ENV is a env variable which is set to the currently active environment path
set MPLBASEDIRLIST=%CONDA_PREFIX%\Library\;.
# build the wheel
python setup.py bdist_wheel
This code was taken from https://matplotlib.org/users/installing.html#conda-packages. Hope this helps.
Thanks
Michael

Categories