You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly-Dash 频繁出现OSError: [WinError 87] 参数不正确的问题求助

Plotly-Dash 频繁出现OSError: [WinError 87] 参数不正确的问题求助

我在网上找了很多类似的问题,但都和我的情况不完全匹配,试了一些解决方案也不管用。

我正在开发一个Plotly-Dash应用,已经创建了虚拟环境并安装了所有需要的包:

  • Python安装目录:D:\Working_Directory

这个应用有时候能正常运行,但大多数时候会抛出这个错误:

OSError: [WinError 87] The parameter is incorrect

卸载Plotly之后重新安装,能正常用几次,但过段时间又会出现同样的错误。

我的代码如下:

import plotly.express as px
import pandas as pd
from dash import Dash, dcc, html, Input, Output

# Reading excel
df = pd.read_excel('Sample - Superstore.xls')
app = Dash(__name__)

app.layout = html.Div([

    # Nav-bar
    html.Div([
        html.H1("Superstore Sales Dashboard")
    ]),

    # Page-content
    html.Div([
        # Graphs
        html.Div([
            html.Div(children=[
                html.Div([
                    dcc.Graph(id='bar-chart', responsive=True, className='graph')
                ]),
                html.Div([
                    dcc.Graph(id="line-chart", responsive=True, className='graph')
                ])
            ], style={'display': 'flex', 'justifyContent': 'space-between'}),
        ]),

        # Vertical-line-separator
        html.Div(className='vl'),

        # Selectors
        html.Div([
            html.Div([
                html.H2("Select Region: "),
                dcc.Dropdown(df['Region'].unique(), df['Region'].unique()[0], id='region-selector')
            ]),

            html.Hr(className='hl'),

            html.Div([
                html.H2("Select Category: "),
                dcc.Checklist(df['Category'].unique(), df['Category'].unique(), id='category-selector',
                              labelStyle={'font-size': '18px'})
            ])
        ])
    ], className='outer-container'),
])

# Callbacks
# Callback for bar chart
@app.callback(
    Output('bar-chart', 'figure'),
    Input('region-selector', 'value'),
    Input('category-selector', 'value')
)
def update_bar_graph(selected_region, selected_categories):
    filtered_df = df[(df['Region'] == selected_region) & (df['Category'].isin(selected_categories))]
    bar_chart = px.bar(filtered_df, x='Category', y='Sales', title='Sales by Category', color='Category')
    return bar_chart

# Callback to update the line chart based on bar graph click and selected region and categories
@app.callback(Output('line-chart', 'figure'),
              Input('bar-chart', 'clickData'),
              Input('region-selector', 'value'),
              Input('category-selector', 'value'))
def update_line_chart(clickData, selected_region, selected_categories):
    filtered_df = df[(df['Region'] == selected_region) & (df['Category'].isin(selected_categories))]
    category = 'Overall Category'
    if clickData:
        category = clickData['points'][0]['x']
        filtered_df = filtered_df[filtered_df['Category'] == category]
    # Sort the filtered dataframe by Order Date
    filtered_df = filtered_df.sort_values(by='Order Date')
    line_fig = px.line(filtered_df, x='Order Date', y='Sales', color='Category', title=f'{category} Sales Over Time')
    return line_fig

if __name__ == '__main__':
    app.run(debug=True)

完整的错误日志:

Traceback (most recent call last):  
File "D:\OneDrive\Desktop\Capabilities\.venv\Lib\site-packages\plotly\express\_core.py", line 942, in apply_default_cascade  
args["template"] = pio.templates[args["template"]]  
File "D:\OneDrive\Desktop\Capabilities\.venv\Lib\site-packages\plotly\io\_templates.py", line 74, in __getitem__
File "<frozen importlib._bootstrap>", line 1412, in _handle_fromlist  
File "D:\OneDrive\Desktop\Capabilities\.venv\Lib\site-packages\_plotly_utils\importers.py", line 36, in __getattr__
    return importlib.import_module(rel_module, parent_name)  
File "D:\Working Directory\Python312\Lib\importlib\__init__.py", line 90, in import_module 
return _bootstrap._gcd_import(name[level:], package, level)  
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import  
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load  
File "<frozen importlib._bootstrap>", line 1322, in _find_and_load_unlocked  
File "<frozen importlib._bootstrap>", line 1262, in _find_spec  
File "<frozen importlib._bootstrap_external>", line 1525, in find_spec  
File "<frozen importlib._bootstrap_external>", line 1499, in _get_spec  
File "<frozen importlib._bootstrap_external>", line 1602, in find_spec  
File "<frozen importlib._bootstrap_external>", line 1645, in _fill_cache   
OSError: [WinError 87] The parameter is incorrect: 
'D:\\OneDrive\\Desktop\\Capabilities\\.venv\\Lib\\site-packages\\plotly\\graph_objs\\layout'

另外,当我在另一个应用中使用dash-bootstrap-components(dbc)时,这个错误出现得更加频繁。

我猜测问题可能和importlib库有关,但不知道具体该怎么解决,希望有人能帮忙指点一下,谢谢!


备注:内容来源于stack exchange,提问作者SnowGirl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 12:35:28