Search code examples
pythonanacondapyinstallerexecommand-prompt

Pyinstaller exe file gives 'plotly' has no attribute 'express' error


I have some code in Python (Jupyter Notebooks in my Anaconda environment). The file is called myFile.py:

import math
import pandas as pd
pd.options.mode.chained_assignment = None  # default='warn'
import plotly

DF = pd.read_csv('My CSV data file path')
myCol = DF['Temp'].value_counts(normalize = True)
fig = plotly.express.bar(myCol)
fig.show()

My code runs exactly as expected in python.

I used pyinstaller to successfully convert this code into an exe file (pyinstaller myFile.py). However, when I run the exe file, I get an error:

File "_plotly_utils\importers.py", line 39 in __getattr__
AttributeError: module 'plotly' has no attribute 'express'
Failed to execute script due to unhandled exception. 

Here's what I tried:

  1. I use the --onefile option with pyinstaller but that did not work.
  2. I imported plotly.express as a hidden import but I got the same error (pyinstaller --hidden-import plotly.express myFile.py)
  3. I copied the entire plotly package from my anaconda environment and pasted it in the same directory as myFile.exe but I got the same error.
  4. I updated my version of plotly with the newest version using conda update plotly. It still didn't solve the issue.
  5. I used the accepted solution posted here pyinstaller fails with plotly but I still got the same error.

My version of plotly is version 5.9.0.


Solution

  • So I ended up resolving the issue. I just created a new anaconda environment using conda create --name <my-env> and then I reinstalled Pandas, plotly and pyinstaller. Using the command pyinstaller myFile.py ended up creating the exe. This time, running the exe gave no error/issues. I suspect the reason for the error last time was because of some dependency/library/module conflicts which prevented the exe from recognizing plotly.