How to Fix the Python Error: modulenotfounderror: no module named 'plotly'

Python is an incredibly powerful language. It combines easy-to-use syntax with some expertly designed data types. It even provides some impressive speed benchmarks when compared with other languages. But perhaps the most impressive thing about the language is the extent to which it can be extended with additional libraries. Math-related libraries like NumPy, Pandas and Plotly are particularly popular. However, expanding Python’s capabilities like that can bring some unexpected errors. These errors include the modulenotfounderror: no module named ‘plotly’ error. This is generally a fairly easy error to fix. But we first need to examine exactly what the error message means.

What Does the Error Mean?

Plotly, like most of the advanced math libraries, isn’t included with Python’s standard distributions. When you see the modulenotfounderror: no module named ‘plotly’ error it generally indicates that you simply need to install Plotly. However, there’s also a possibility that Plotly was installed but it’s not being detected by Python.

More on Modules

Python’s libraries are also known as modules. They’re typically additional .py files that are automatically appended into your code once you successfully import them. This is somewhat analogous to the way webpages add the content within JavaScript or CSS files. This process is typically even easier in Python code since you’re loading modules from internal storage rather than over a network. Consider the following code as an example.

import plotly.express as plt
test = plt.line(x=[1,2, 3], y=[1, 2, 3])
print(test)

This is a simple piece of code that should import the Plotly module, create a figure instance, and then print the result to your screen. Note that the import statement doesn’t specify where Python’s runtime should look for Plotly. This is because Python’s system has a special setting that details where module files are stored. This setting is the PYTHONPATH.

A special internal path allows for a greater level of platform independence. Different operating systems handle directory structures in different ways. But with PYTHONPATH, programmers can let the runtime handle a lot of the work involved with file locations. But as you’ll soon see, the PYTHONPATH isn’t always foolproof.

Fixing the Module Not Found Error

If you tried to run the previous code then you most likely saw a “modulenotfounderror: no module named ‘plotly'” error. This typically occurs due to one of two possible causes. The first is an issue with the previously mentioned path. Operating systems that ship with Python should have PYTHONPATH set by default. As such, this problem typically occurs in Windows systems where Python’s installation was performed manually.

Fixing this issue involves first finding the directory where you installed Python. The directory is typically stored at the top level of the C drive. This is usually labeled according to Python’s version. For example, Python’s 2.7 version would typically be found in the C:\Python27 directory. You can also use the interpreter to report where it’s running from with the following code.

python -c “import os, sys; print(os.path.dirname(sys.executable))”

Wherever Python’s runtime is installed, you’ll next want to add that directory to your path by going to My Computer > Properties > Advanced System Settings > Environment Variables > System variables. Create a new variable called PYTHONPATH and then include Python’s main folder and all subdirectories within it. For example, with a 2.7 install you might enter your PYTHONPATH as C:\Python27\Lib;C:\Python27\include; and so on.

After this step, you should reboot Windows to make sure the new path variable is properly applied. If you still receive the module not found error then you’re most likely looking at the second possible cause – a missing Plotly installation. It essentially means that Plotly wasn’t installed or that the installation process was incomplete. This often occurs when installing modules through binary packages from the Web rather than using Python’s native installers.

Thankfully this is also easily solved. At this point, you have a properly set PYTHONPATH. This means that a module installation tool called PIP can be called from your terminal or console. You can verify this by bringing up a command line prompt and typing pip. However, some systems differentiate Python’s 2.x and 3.x installs. If pip isn’t running on your command line then you might need to refer to it as pip2 or pip3. Just make sure that you’re not using pip2 if you intend to write code for Python’s 3.x releases. And conversely, don’t call pip3 if you’re writing for 2.x. Once you’ve verified that you can start pip you simply need to type the following code on your command line.

pip install plotly

Of course, you’ll want to use whatever PIP is available on your system. This will most likely simply be pip. But, again, it could be pip2 or pip3. Whichever build of PIP you’re using, it should download and install Plotly onto your PYTHONPATH. This means that Plotly will automatically be picked up by Python once you use an import statement for it in your code. We can try a simple example again. Running the following code should properly import Plotly and run a quick test calculation.

import plotly.express as plt
test = plt.line(x=[1,2, 3], y=[1, 2, 3])
print(test)

The data should be properly processed and printed to screen. The Python error won’t appear anymore now that the runtime knows where to find the Plotly module.

How to Fix the Python Error: modulenotfounderror: no module named 'plotly'
Scroll to top