我有一些 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.
这是我尝试过的:
--onefile
选项,但这不起作用。pyinstaller --hidden-import plotly.express myFile.py
)myFile.exe
相同的目录中,但我得到了相同的错误。conda update plotly
将我的plotly 版本更新为最新版本。还是没有解决问题。
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')],
pyinstaller myFile.spec