Black Lives Matter. Please consider donating to Black Girls Code today.

Installing Plotly (ModuleNotFoundError: No module named 'plotly')

I have followed the Plotly installation instructions using conda and pip and both lead to the following Python traceback error: ModuleNotFoundError : No module named ‘plotly’

I have been Googling for more than an hour to find answer to this and no matter what I have tried, I can’t resolve this error. The simplest test case I have done is:

  1. create a new conda environment
  2. activate said environment
  3. $ conda install -c plotly plotly=4.3.0
  4. $ jupyter notebook
  5. create test notebook
  6. run notebook which only contains: import plotly

I’m using Python 3.7.3.

What am I missing? Thanks for your help.

Sorry to hear this @adiadidas15. It’s hard to know what’s going on, so here are a couple of questions which might help you debugging.

What happens if you don’t specify the version number of plotly (just conda install -c plotly plotly)?

Also, does the conda install command raise any error or does it complete successfully?

You can also check in the notebook the sys.path (import sys; sys.path) and check whether the directory where conda installed plotly is in the list of the sys.path.

Finally, did you try to install another package with this method just to check that it’s not something wrong with your version of conda?

Did you already have an older version of plotly installed? (With a fresh environment this shouldn’t be a problem, but…)

Hello Emmanuelle,
When I execute conda install -c plotly plotly, I get # All requested packages already installed.

conda install has never returned an error in my many attempts to get plotly installed.

I’m not sure how to find where conda “installs” plotly, so I can’t conform that is in the sys.path.
But when I execute conda list, plotly is listed.

what if you try to remove plotly and install it again?

I have tried removing it several times and got the same result.

But when you remove it then during a conda install it should tell you in which directory it’s installing the package? I’m not using conda much but that’s what pip does.

To answer some of your other questions, I may have (I’m not sure) first installed plotly using Anaconda Navigator rather than the Anaconda Prompt. I have successfully installed other packages using Anaconda Prompt such as selenium, pandas, numpy, etc.

Conda tells me the path to my virtual environment when I install a package. It doesn’t tell me a path to where the package is installed. I’m happy to use pip instead of conda if that would help.

Which OS are you using? With windows it can be hard without anaconda. Otherwise (linux and mac) trying with pip is a good idea for debugging. I would start by uninstalling plotly with both conda and pip as described in the troubleshooting guide https://plot.ly/python/troubleshooting/#import-problems

I’m using Windows. I’ll follow the troubleshooting guide and report my results.

Okay, I executed conda uninstall plotly without getting any errors. FYI, the troubleshooting guide gives the wrong command. It instructs pip remove plotly but I believe it should be pip uninstall plotly. I got an error with the former (ERROR: unknown command "remove") and got WARNING: Skipping plotly as it is not installed. with the latter.

I then executed conda install -c plotly plotly=4.3.0 and didn’t get any errors.

Running the Python command import plotly still gives the same error.

This looks like a classic PYTHONPATH issue, Python/Anaconda installation on Windows can be tricky.

In a cmd terminal with an activated environment:

  • Does where python gives you the correct path to a python.exe in a folder named after your environment?
  • Does pip freeze list plotly?

Hello Batalex,
With my virtual environment activated, where python does reveal python.exe to be present in my environment directory. Also, pip freeze does list plotly.

Can you try outside of the notebook, in a normal python or ipython shell launched within your environment?

Well I have some new information. When I run import plotly in an IPython window via VS Code I get the same result as in a Jupyter notebook. But when I run this line in a Python terminal via VS Code it executes without an error!

Hum, is jupyter installed in the virtual environment? If not, then the jupyter/ipython command would use the global python installation instead of the venv’s. Can you try in your venv to launch jupyter with python -m jupyter notebook?

After running into this problem with another package just now, I started doing some more Googling. Running import sys then print(sys.path) in both the VS Code terminal and the VS Code IPython REPL gives different paths. The terminal lists my virtual environment directories, but the IPython session does not. Therefore my problem is figuring out how to configure Anaconda so that it will launch IPython sessions with the same virtual environment as my terminal. I’ll post this question on stackoverflow since it is clearly not specific to plotly. Thanks for your help!

OK, good luck then, hope you can solve the issue. A different solution could be to reinstall Anaconda from scratch, they provide plotly 4.1 which is not the most recent version but maybe it’s enough for your needs?

For posterity’s sake, here’s my SO question and answer. Ultimately the problem was a silly mistake on my part. I hadn’t installed jupyter in my virtual environment, so whenever I started a session based on IPython, it was failing to do so in my activated environment. It would then look for other virtual environments to use, in my case choosing the base conda envirornment. This environment does not have plotly installed, but it had all the other packages I needed.

Thanks for your help @Emmanuelle & @Batalex!