首页 > 解决方案 > How to fix "No such file or directory" occuring at exe using plotly

问题描述

I am trying to write a python software that uses plotly. The script works well when I execute it with the console. Afterward, I created an exe file using Pyinstaller. But the execution of the exe fails with the error message

FileNotFoundError: No such file or directory "Path\To\PythonScript\plotly\package_data\plotly.min.js"

Here is some information about my system:


import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, plot

xValues = [1, 2, 3]
yValues1 = [5, 5, 5]
yValues2 = [7, 6, 7]

firstTrace = go.Scatter( x = xValues, y = yValues1, mode='lines+markers', name='first' )
secondTrace = go.Scatter( x = xValues, y = yValues2, mode='lines+markers', name='second' )

plottedData = [firstTrace, secondTrace]

plot( plottedData )

What should I do, so the exe runs without this error?

标签: pythonruntime-errorplotlypyinstaller

解决方案


通常,PyInstaller 不会处理所有模块依赖项,尤其是当它们使用附加文件时。您需要手动将它们提供给您的最终可执行文件。

所以对于plotly,它使用一个名为 的目录package_data,其中包含模板文件、js 文件等。所以你需要使用Tree类将它添加到你的可执行文件中。编辑您生成的规范文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(
    ...
)
a.datas += Tree("<Python_Path>/Lib/site-packages/plotly/package_data/", "./plotly/package_data")
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
...

推荐阅读