Pyinstaller exe 文件给出“plotly”没有属性“express”错误

问题描述 投票:0回答:1

我有一些 Python 代码(我的 Anaconda 环境中的 Jupyter Notebooks)。该文件名为

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()

我的代码在 python 中完全按照预期运行。

我使用pyinstaller成功地将这段代码转换成exe文件(

pyinstaller myFile.py
)。但是,当我运行exe文件时,出现错误:

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

这是我尝试过的:

  1. 我使用 pyinstaller 的
    --onefile
    选项,但这不起作用。
  2. 我导入了plotly.express作为隐藏导入,但我得到了同样的错误(
    pyinstaller --hidden-import plotly.express myFile.py
  3. 我从 anaconda 环境中复制了整个plotly 包并将其粘贴到与
    myFile.exe
    相同的目录中,但我得到了相同的错误。
  4. 我使用
    conda update plotly
    将我的plotly 版本更新为最新版本。还是没有解决问题。
  5. 我使用了此处发布的已接受的解决方案pyinstaller 失败,但我仍然遇到相同的错误。
我的plotly版本是5.9.0版本。

python anaconda pyinstaller exe command-prompt
1个回答
0
投票
使用命令

pyi-makespec options myFile.py

生成
.spec
文件。然后在
a
部分你会发现类似这样的内容:

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, )
您需要在

plotly

中添加
datas=[]
的路径。所以激活你正在使用的Anaconda环境并执行这个命令来找到
plotly
的安装路径:
python -c "import plotly; print(plotly.__path__)"

您将得到如下输出:

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


然后将其插入到

data

参数中。它应该看起来像这样:

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


保存.spec文件并运行命令:

pyinstaller myFile.spec


这应该可以解决问题

© www.soinside.com 2019 - 2024. All rights reserved.