Skip to main content
 首页 » 编程设计

python之为什么我会收到 "Cannot import name"错误

2025年04月02日13leader

我正在尝试在 Bokeh 中显示时间序列图表,但我收到的错误消息是“无法导入名称‘TimeSeries’”。我对 bokeh 和 python 很陌生,所以我们将不胜感激任何帮助。

from yahoo_finance import Share 
import pandas as pd 
from bokeh.charts import TimeSeries, output_file, show 
 
# Getting stocks from Yahoo Finance 
yahoo = Share('YHOO') 
google = Share('GOOGL') 
 
# User selects stocks 
stock = input("Enter stock name: ") 
choice = Share(stock) 
 
yahoo.refresh() 
choice.refresh() 
 
# gets stock data for a desired Stock 
yahooData = choice.get_historical('2016-05-01', '2016-05-23') 
 
# all dates are stored here 
date = []  
 
# all stock prices are stored here 
data = [] 
 
# sanity check to see if the thing works 
for i in range(10): 
    date.append(yahooData[i]['Date']) 
    data.append(yahooData[i]['Adj_Close']) 
    print(date[i], data[i], sep='  ') 
 
# turns the two lists into a data frame 
stock_data = pd.DataFrame({'Dates': date , 'Prices' : data}) 
stock_data 
 
# Where things go to s**t 
p = TimeSeries(stock_data, index = 'Dates', legend = 'True', title = "Stock Chart", ylabel = 'Prices') 
output_file("Stock_chart.html") 
show(p) 

产生这个错误:

ImportError Traceback(最后一次调用) 在 () 1 来自 yahoo_finance import 分享 2 将 Pandas 导入为 pd ----> 3 从 bokeh.charts 导入 TimeSeries、output_file、show 4个 5 # 从雅虎财经获取股票

ImportError: 无法导入名称“TimeSeries”

请您参考如下方法:

可能您使用的是 bokeh 0.10.x 或以下版本。从 0.11.1 开始添加了对 Bokeh 中 show、output_file 的支持。将您的 Bokeh 版本更新为 0.12.4,一切就绪。