月下孤井 发表于 2022-9-17 08:59:15

python打包akshare出错怎么解决

就是程序里面需要用到akshare来获取数据, 但是程序写好后pyinstaller打包好后点开生成好的软件会报错, 后面试了一下, 哪怕程序里只有import akshare as ak一句代码也会报错

import akshare as ak
print(123)

哪位大佬帮忙解决一下, 上面两句代码生成一个.exe文件, 并成功输出123,

ZhKQYu 发表于 2022-9-17 09:38:21

报什么错,贴出来看看

月下孤井 发表于 2022-9-17 09:48:06

ZhKQYu 发表于 2022-9-17 09:38
报什么错,贴出来看看

第一个错通过https://blog.csdn.net/rainolt/article/details/121449660教的方法解决了, 后面又出来一个错:
Traceback (most recent call last):
File "test_5.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "akshare\__init__.py", line 2843, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "akshare\index\index_kq_fz.py", line 11, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "akshare\utils\ak_session.py", line 8, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "requests_cache\__init__.py", line 7, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "requests_cache\backends\__init__.py", line 7, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "requests_cache\backends\base.py", line 18, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "requests_cache\serializers\__init__.py", line 6, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "requests_cache\serializers\preconf.py", line 122, in <module>
File "requests_cache\serializers\pipeline.py", line 44, in __init__
File "requests_cache\serializers\pipeline.py", line 44, in <listcomp>
AttributeError: type object 'Placeholder' has no attribute 'loads'
Failed to execute script 'test_5' due to unhandled exception!

这个找不到解决的方法了

ZhKQYu 发表于 2022-9-17 11:00:11

月下孤井 发表于 2022-9-17 09:48
第一个错通过https://blog.csdn.net/rainolt/article/details/121449660教的方法解决了, 后面又出来一个 ...

要不发一下源码 我打包试试

月下孤井 发表于 2022-9-17 16:47:15

ZhKQYu 发表于 2022-9-17 11:00
要不发一下源码 我打包试试

import akshare as ak
import datetime
import pandas as pd
def get_start_date():
    time_year = datetime.datetime.now().year
    time_year_start = datetime.datetime.now().year - 1
    time_month = str(datetime.datetime.now()).split().split('-')
    time_day = str(datetime.datetime.now()).split().split('-')
    return str(time_year_start) + time_month + time_day, str(time_year) + time_month + time_day
def get_A_code():
    shanghai_A_code = ak.stock_sh_a_spot_em()
    shanghai_A_code.dropna(inplace=True)
    shanghai_A_code = shanghai_A_code[['代码', '名称']]
    shanghai_A_code = shanghai_A_code.str != '68']
    shenzhen_A_code = ak.stock_sz_a_spot_em()
    shenzhen_A_code.dropna(inplace=True)
    shenzhen_A_code = shenzhen_A_code[['代码', '名称']]
    shenzhen_A_code = shenzhen_A_code.str != '30']
    A_code = pd.concat(, ignore_index=True)
    return A_code
def parse_code_month(stock_code):
    df = ak.stock_zh_a_hist(symbol=stock_code, period="monthly", start_date=get_start_date(), end_date=get_start_date(), adjust="qfq")[['日期', '开盘', '收盘', '最高', '最低', '涨跌幅']]
    df['代码'] = stock_code
    df.set_index('代码', inplace=True)
    df.to_csv(r'股票数据.csv', encoding='ANSI', mode='a', header=False)
    print(df)
if __name__ == '__main__':
    for stock_code in get_A_code()['代码'].tolist():
      parse_code_month(stock_code)

cjh4312@163.com 发表于 2022-10-18 11:50:42

本帖最后由 cjh4312@163.com 于 2022-10-18 11:59 编辑

我和你遇到同样的问题。问题呢,我估计还是akshare,它也用到了好多第三方库,打包的时候这些库并没有打包进来。
你把cattr和cattrs复制进来,包括akshare本身也复制过来。就ok了。
第一个问题(py_mini_racer)缺的也是第三方库。

月下孤井 发表于 2022-10-18 18:49:59

好的,谢谢,我试一下
页: [1]
查看完整版本: python打包akshare出错怎么解决