制作Python神经元数据下载可执行文件:解决Auto Py to EXE打包失效问题
问题根源
- 你猜测的没错,故障完全来自ipywidget组件:它是专门为Jupyter Notebook/Lab环境设计的交互组件,依赖Jupyter内核和浏览器前端才能运行,普通桌面执行环境没有对应的支撑,所以双击EXE后没有任何响应。
- Auto Py to EXE打包时会自动检测导入的依赖,ipywidget关联了整个Jupyter、PyQt、甚至数据分析相关的冗余库,所以才会生成上千个文件、体积超过1GB。
可行改造方案
替换ipywidget为轻量桌面交互组件PySimpleGUI,改造成本极低,完全适配打包需求。
步骤1:安装依赖
执行命令:pip install pysimplegui
步骤2:替换交互部分代码
将原来的ipywidget相关代码全部替换为以下内容,你原来的API请求、数据解析、生成CSV的代码可以直接复用:
import PySimpleGUI as sg import requests import pandas as pd # 直接复用你原来的下拉选项,已补全原代码中遗漏的两处逗号 brain_regions = ['abdominal ganglion', 'accessory lobe', 'accessory olfactory bulb', 'adult subesophageal zone', 'amygdala', 'antenna', 'antennal lobe', 'anterior olfactory nucleus', 'basal forbrain', 'basal ganglia', 'brainstem', 'Central complex', 'Central nervous system', 'cerebellum', 'cerebral ganglion', 'Cochlea', 'corpus callosum', 'cortex', 'electrosensory lobe', 'endocrine system', 'enthorinal cortex', 'eye circuit', 'forebrain', 'fornix', 'ganglion', 'hippocampus', 'hypothalamus', 'lateral complex', 'lateral horn', 'lateral line organ', 'left', 'Left Adult Central Complex', 'Left Mushroom Body', 'main olfactory bulb', 'meninges', 'mesencephalon', 'myelencephalon', 'neocortex', 'nuchal organs', 'olfactory cortex', 'olfactory pit', 'optic lobe', 'pallium', 'parasubiculum', ' peptidergic circuit', 'peripheral nervous system', 'pharyngeal nervous system', 'pons', 'Pro-subiculum', 'protocerebrum', 'retina', 'retinorecipient mesencephalon and diencephalon', 'Right Adult Central Complex', 'Right Mushroom Body', 'somatic nervous system', 'spinal cord', 'stomatogastric ganglion', 'subesophageal ganglion', 'subesophageal zone-(SEZ)', 'subiculum', 'subpallium', 'Subventricular zone', 'thalamus', 'ventral nerve cord', 'ventral striatum', 'ventral thalamus', 'ventrolateral neuropils', 'Not reported'] animals = ['African wild dog', 'agouti', 'Apis mellifera', 'Aplysia', 'Axolotl', 'Baboon', 'Blind mole-rat', 'blowfly', 'Blue wildebeest', 'Bonobo', 'bottlenose dolphin', 'C. elegans', 'Calango lizard', 'capuchin monkey', 'Caracal', 'cat', 'cheetah', 'chicken', 'chimpanzee', 'Clam worm', 'clouded leopard', 'Crab', 'cricket', 'Crisia eburnea', 'Domestic dog', 'domestic pig', 'dragonfly', 'drosophila melanogaster', 'drosophila sechellia', 'elephant', 'ferret', 'giraffe', 'goldfish', 'grasshopper', 'Greater kudu', 'guinea pig', 'Hamster', 'human', 'humpback whale', 'Lemur', 'leopard', 'Lion', 'locust', 'manatee', 'minke whale', 'Mongoose', 'monkey', 'Mormyrid fish', 'moth', 'mouse', 'pouched lamprey', 'Praying mantis (Hierodula membranacea)', 'Praying mantis (Hierodula membranacea)', 'proechimys', 'rabbit', 'Rana esculenta', 'Ranitomeya imitator', 'rat', 'Rhinella arenarum', 'Ruddy turnstone', 'salamander', 'Scinax granulatus', 'Sea lamprey', 'Semipalmated plover', 'Semipalmated sandpiper', 'sheep', 'Silkmoth', 'spiny lobster', 'Stellers Sculpin', 'Tiger', 'Toadfish', 'Treeshrew', 'turtle', 'Wallaby', 'Xenopus laevis', 'Xenopus tropicalis', 'Zebra', 'zebra finch', 'zebrafish', 'Not reported'] cell_types = ['Glia', 'interneuron', 'principal cell', 'sensory', 'Not reported'] # 构建桌面交互界面 layout = [ [sg.Text('脑区:'), sg.Combo(brain_regions, default_value='cerebellum', key='-BRAIN-', size=(50,1))], [sg.Text('物种:'), sg.Combo(animals, default_value='mouse', key='-ANIMAL-', size=(50,1))], [sg.Text('细胞类型:'), sg.Combo(cell_types, default_value='principal cell', key='-CELL-', size=(50,1))], [sg.Button('开始下载'), sg.Button('退出')], [sg.Output(size=(80,20))] ] window = sg.Window('神经元数据下载工具', layout) while True: event, values = window.read() if event == sg.WIN_CLOSED or event == '退出': break if event == '开始下载': # 获取用户选择的参数 str1 = values['-BRAIN-'] str2 = values['-ANIMAL-'] str3 = values['-CELL-'] # 以下直接复用你原有的API请求、数据解析代码即可 url = 'http://neuromorpho.org/api/neuron/select' params = { 'page' : 0, 'q' : 'brain_region:' + str1, 'fq' : [ 'cell_type:' + str3, 'species:' + str2, ] } first_page_response = requests.get(url, params) if first_page_response.status_code in (404,500): print('请求失败,请检查网络或参数设置') continue totalPages = first_page_response.json()['page']['totalPages'] # 此处插入你原有的df_dict构建、多页数据拉取代码 # 数据处理完成后新增一行存储CSV即可 # neurons_df.to_csv('神经元导出数据.csv', index=False, encoding='utf_8_sig') # print('数据导出完成,已保存为 神经元导出数据.csv') window.close()
步骤3:优化打包参数
替换完成后用Auto Py to EXE打包时做以下设置,可将最终EXE体积控制在30-50MB:
- 选择「单文件」模式,不会生成上千个零散文件
- 不需要显示运行日志的话可以勾选「窗口模式(隐藏控制台)」
- 高级选项的「排除模块」中添加
jupyter, ipython, ipywidgets, matplotlib, scipy等你没有用到的冗余库,进一步压缩体积
内容的提问来源于stack exchange,提问作者Zaki
相关产品推荐
相关产品推荐

