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

63 Views Asked by At

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.

2

There are 2 best solutions below

0
Diamoniner12345 On BEST ANSWER

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.

2
Obaskly On

Use the command pyi-makespec options myFile.py to generate a .spec file. Then in a section you will find something like this:

a = Analysis(
    ['options', 'myFile.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

You need to add the path to plotly in datas=[]. So Activate the Anaconda environment you are using and execute this command to find the installation path of plotly:

python -c "import plotly; print(plotly.__path__)"

You will get an output like this:

['C:\\Users\\Username\\Anaconda3\\envs\\YourEnv\\Lib\\site-packages\\plotly']

Then insert it in data parameter. It should look like this:

datas=[('C:\\Users\\Username\\Anaconda3\\envs\\YourEnv\\Lib\\site-packages\\plotly', 'plotly')],

Save the .spec file and run the command: pyinstaller myFile.spec

This should solve the problem