pyinstaller打包报错
问题
自己写了一个运行代码希望通过pyinstaller进行打包,但是尝试多次始终报错,以为是电脑的环境配置可能有问题换了不同的电脑尝试依然不成功,百度也找不到答案,求教社区大神是什么原因以及有啥方法可以解决。(报错排版稀烂费眼睛先求大佬谅解)
报错如下
151994 INFO: Loading module hook 'hook-zope.interface.py' from 'd:\\anaconda\\lib\\site-packages\\PyInstaller\\hooks'... d:\anaconda\lib\site-packages\win32com\client\makepy.py:369: SyntaxWarning: "is not" with a literal. Did you mean "!="? if path is not '' and not os.path.exists(path): 152406 INFO: Loading module hook 'hook-gevent.py' from 'd:\\anaconda\\lib\\site-packages\\PyInstaller\\hooks'... Traceback (most recent call last): File "d:\anaconda\lib\runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "d:\anaconda\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "D:\Anaconda\Scripts\pyinstaller.exe\__main__.py", line 7, in <module> File "d:\anaconda\lib\site-packages\PyInstaller\__main__.py", line 205, in _console_script_run run() File "d:\anaconda\lib\site-packages\PyInstaller\__main__.py", line 189, in run run_build(pyi_config, spec_file, **vars(args)) File "d:\anaconda\lib\site-packages\PyInstaller\__main__.py", line 61, in run_build PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs) File "d:\anaconda\lib\site-packages\PyInstaller\building\build_main.py", line 1033, in main build(specfile, distpath, workpath, clean_build) File "d:\anaconda\lib\site-packages\PyInstaller\building\build_main.py", line 973, in build exec(code, spec_namespace) File "D:\PythonProject\Shanghai_company.spec", line 4, in <module> a = Analysis( File "d:\anaconda\lib\site-packages\PyInstaller\building\build_main.py", line 466, in __init__ self.__postinit__() File "d:\anaconda\lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__ self.assemble() File "d:\anaconda\lib\site-packages\PyInstaller\building\build_main.py", line 610, in assemble self.graph.process_post_graph_hooks(self) File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 329, in process_post_graph_hooks module_hook.post_graph(analysis) File "d:\anaconda\lib\site-packages\PyInstaller\depend\imphook.py", line 450, in post_graph self._process_hidden_imports() File "d:\anaconda\lib\site-packages\PyInstaller\depend\imphook.py", line 506, in _process_hidden_imports self.module_graph.import_hook(import_module_name, caller) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1345, in import_hook submodule = self._safe_import_module(head, mname, submodule) File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 479, in _safe_import_module return super()._safe_import_module(module_basename, module_name, parent_package) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1893, in _safe_import_module self._process_imports(n) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2704, in _process_imports target_modules = self._safe_import_hook(*import_info, **kwargs) File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 432, in _safe_import_hook return super()._safe_import_hook(target_module_partname, source_module, target_attr_names, level, edge_attr) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2370, in _safe_import_hook self.import_hook( File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1371, in import_hook for target_submodule in self._import_importable_package_submodules( File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1621, in _import_importable_package_submodules submodule = self._safe_import_module( File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 479, in _safe_import_module return super()._safe_import_module(module_basename, module_name, parent_package) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1893, in _safe_import_module self._process_imports(n) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2704, in _process_imports target_modules = self._safe_import_hook(*import_info, **kwargs) File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 432, in _safe_import_hook return super()._safe_import_hook(target_module_partname, source_module, target_attr_names, level, edge_attr) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2161, in _safe_import_hook target_modules = self.import_hook( File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1345, in import_hook submodule = self._safe_import_module(head, mname, submodule) File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 479, in _safe_import_module return super()._safe_import_module(module_basename, module_name, parent_package) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1893, in _safe_import_module self._process_imports(n) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2704, in _process_imports target_modules = self._safe_import_hook(*import_info, **kwargs) File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 432, in _safe_import_hook return super()._safe_import_hook(target_module_partname, source_module, target_attr_names, level, edge_attr) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2161, in _safe_import_hook target_modules = self.import_hook( File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1332, in import_hook target_package, target_module_partname = self._find_head_package( File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1511, in _find_head_package target_package = self._safe_import_module( File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 479, in _safe_import_module return super()._safe_import_module(module_basename, module_name, parent_package) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1893, in _safe_import_module self._process_imports(n) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2704, in _process_imports target_modules = self._safe_import_hook(*import_info, **kwargs) File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 432, in _safe_import_hook return super()._safe_import_hook(target_module_partname, source_module, target_attr_names, level, edge_attr) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2161, in _safe_import_hook target_modules = self.import_hook( File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1332, in import_hook target_package, target_module_partname = self._find_head_package( File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1511, in _find_head_package target_package = self._safe_import_module( File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 479, in _safe_import_module return super()._safe_import_module(module_basename, module_name, parent_package) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1893, in _safe_import_module self._process_imports(n) File "d:\anaconda\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2704, in _process_imports target_modules = self._safe_import_hook(*import_info, **kwargs) File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 368, in _safe_import_hook excluded_imports = self._find_all_excluded_imports(source_module.identifier) File "d:\anaconda\lib\site-packages\PyInstaller\depend\analysis.py", line 356, in _find_all_excluded_imports excluded_imports.update(module_hook.excludedimports) File "d:\anaconda\lib\site-packages\PyInstaller\depend\imphook.py", line 316, in __getattr__ self._load_hook_module() File "d:\anaconda\lib\site-packages\PyInstaller\depend\imphook.py", line 383, in _load_hook_module self._hook_module = importlib_load_source(self.hook_module_name, self.hook_filename) File "d:\anaconda\lib\site-packages\PyInstaller\compat.py", line 626, in importlib_load_source return mod_loader.load_module() File "<frozen importlib._bootstrap_external>", line 462, in _check_name_wrapper File "<frozen importlib._bootstrap_external>", line 962, in load_module File "<frozen importlib._bootstrap_external>", line 787, in load_module File "<frozen importlib._bootstrap>", line 265, in _load_module_shim File "<frozen importlib._bootstrap>", line 702, in _load File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "d:\anaconda\lib\site-packages\PyInstaller\hooks\hook-gevent.py", line 24, in <module> datas += copy_metadata('gevent', recursive=True) File "d:\anaconda\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 991, in copy_metadata requirements = [req.name for req in requirements if req.marker is None or req.marker.evaluate()] File "d:\anaconda\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 991, in <listcomp> requirements = [req.name for req in requirements if req.marker is None or req.marker.evaluate()] File "d:\anaconda\lib\site-packages\packaging\markers.py", line 328, in evaluate return _evaluate_markers(self._markers, current_environment) File "d:\anaconda\lib\site-packages\packaging\markers.py", line 244, in _evaluate_markers lhs_value = _get_env(environment, lhs.value) File "d:\anaconda\lib\site-packages\packaging\markers.py", line 224, in _get_env raise UndefinedEnvironmentName( packaging.markers.UndefinedEnvironmentName: 'extra' does not exist in evaluation environment.
* 补充脚本代码
import pandas as pd
import numpy as np
import os
# import time
# start = time.perf_counter()
def fill_empty(data_frame, column):
for shape in range(data_frame.shape[0]):
if pd.isnull(data_frame.iloc[shape, column]) == True:
data_frame.iloc[shape, column] = data_frame.iloc[shape - 1, column]
# data_frame.iloc[shape, 0] = count 考虑删除
return data_frame
# 填充空值
def cleaning_useless(data_frame, column_name):
data_frame.drop(data_frame[ (data_frame[ column_name ] == 0) |
(pd.isnull ( data_frame[ column_name ]) == True) ].index,
inplace=True)
data_frame.reset_index(drop=True, inplace=True)
return data_frame
# 删除无用数据(凭证金额为0及空值)
def is_numeric(value):
try:
float(value)
return True
except:
return False
# 判断是否是数字值
'基本设置'
operating_time = input('输入过账日期:')
# 设定入账日期 20230927
setting_deltime = input('结账期间:')
# 指定时间区间 09.27-10.28
company_name = '上海销售'
path = r'C:\Users\user\Desktop\test\操作文件'
# 操作文件存放路径
save_path = r'C:\Users\user\Desktop\test\结果文件'
# 结果文件存放路径
# setting_path = r'C:\Users\user\Desktop\test\基础设定文件'
# 设定模板文件路径
# 读取关联交易模版备用
# setting_file =[i for i in os.listdir(setting_path) if (i.startswith('~$') == False) and ('导入模版' in i)]
# input_mod = pd.read_excel(os.path.join(setting_path, setting_file[0]), usecols=range(10),header=0, dtype='str')
c1 = ['凭证模板(5张凭证)','凭证模板 (跨公司代码)']
c2 = ['凭证模板 (跨公司代码)']
c3 = ['凭证模板(5张凭证)','凭证模板(合并纳税)']
c4 = ['凭证模板 (跨法人) '] # 涉及跨法人
f1 = ['凭证模板(第三方手续费-本部)','凭证模板(跨公司代码)']
f2 = ['凭证模板(跨公司代码)']
f3 = ['凭证模板(第三方手续费-本部)','凭证模板(跨法人站)'] # 涉及跨法人
f4 = ['凭证模板(第三方手续费-本部)','凭证模板(合并纳税)']
title = ['标识', '凭证日期', '过账日期', '凭证类型', '公司代码', '货币', '汇率', '抬头参照', '抬头文本', '页数']
# 只引入非关联交易和关联交易之间的差异title,以备进行合并
input_mod = pd.DataFrame(columns=title)
head = [
'公司代码',
'记账码',
'总账科目',
'供应商',
'客户',
'利润中心',
'成本中心',
'贸易伙伴',
'原因代码'
]
head_dtype = {}
for heads in head:
head_dtype[heads] = 'str'
# 指定列数据列文本类型,生成dict
sheet_type = {
'凭证模板(第三方手续费-本部)':0,
'凭证模板(5张凭证)':0,
'凭证模板(合并纳税)':0,
'凭证模板 (跨公司代码)':0,
'凭证模板(跨公司代码)':0,
'凭证模板(第三方手续费-本部)':0,
'凭证模板 (跨法人) ':1,
'凭证模板(跨法人站)':1
}
# 指定基础sheet类型,0为一般模板,1为关联交易模板
for i in os.listdir(path):
print(f'正在处理{i}')
temp = i[:-5].split('-') # 直接去除.xlsx进行分割
file_name = i.split('.')[0]
bill_num = temp[0] # 单号
type = temp[1] # 模板类型
print(f'本文档须使用{locals()[type]}工作表')
load_sheet = locals()[type] # 确定模板类型
main_code = bill_num[2:6] # 公司代码
file_path = os.path.join(path, i)
# 读取数据
count = 0
for i in range( len( load_sheet ) ):
if sheet_type[ load_sheet[i] ] == 0:
count += 1
exec(
f'data_{i} = pd.read_excel(file_path,
sheet_name=load_sheet[i],
dtype=head_dtype,
engine="openpyxl")' )
else:
data_special = pd.read_excel(file_path,
sheet_name=load_sheet[i],
dtype=head_dtype,
engine='openpyxl',
header=None)
# 非关联交易模板
merge_1 = pd.DataFrame()
if count != 0:
for i in range(count):
var_name = f'data_{i}'
temp_pd = locals()[var_name].copy()
sample = fill_empty(temp_pd, temp_pd.columns.get_loc("抬头文本"))
# 填充抬头文本列空值
sample = cleaning_useless(sample, "凭证货币金额")
# 删除无金额的凭证条目
sample["行项目文本"] = sample["抬头文本"]
merge_1 = pd.concat([merge_1, sample]).copy()
columns = [i.strip() for i in merge_1.columns] # 避免模板列索引包含空格,处理相关事项
merge_1.columns = columns
exec(f'del data_{i}') # 循环一次后删除
# 关联交易模板
if 'data_special' in locals():
# 判断关联交易模板是否在本地变量中(),会影响循环是否进行,一次循环结束后应从本地变量中删除
# 获取关联交易模板中全部的公司代码,将关联交易模板中的关键数据,按公司代码合并在一个字典中,字典的键为公司代码对应的index+1(即每个凭证的head行)
index_list = data_special[data_special.iloc[:, 3] == '客户'].index.tolist() # 关键行索引
# 全部凭证初始行,构成一个list
company_codes = []
# 关联交易凭证公司代码构成一个list
head_str = []
# 各凭证初始行前一行中的凭证抬头文本数据
for i in index_list:
head_str.append(data_special.iloc[i - 1, 7]) # 关键行索引抬头文本
company_codes.append(data_special.iloc[i - 1, 3]) # 公司代码
merge_2 = pd.DataFrame()
for i in index_list[:-1]: # 关键行索引至最后一个索引前
temp_pd = data_special.iloc[i: index_list[index_list.index(i) + 1] - 3, :].copy()
temp_pd.columns = temp_pd.iloc[0, :]
temp_pd.reset_index(inplace=True, drop=True)
temp_pd.drop(0, axis=0, inplace=True)
temp_pd['行项目文本'] = head_str[index_list.index(i)]
temp_merge = pd.concat([input_mod, temp_pd], axis=1)
temp_merge['公司代码'] = company_codes[index_list.index(i)]
temp_merge = cleaning_useless(temp_merge, "凭证货币金额")
merge_2 = pd.concat([merge_2, temp_merge], axis=1)
temp_pd = data_special.iloc[index_list[-1]:, :].copy()
temp_pd.columns = temp_pd.iloc[0, :]
temp_pd.reset_index(inplace=True, drop=True)
temp_pd.drop(0, axis=0, inplace=True)
temp_pd['行项目文本'] = head_str[-1]
temp_merge = pd.concat([input_mod, temp_pd], axis=1)
temp_merge['公司代码'] = company_codes[-1]
temp_merge = cleaning_useless(temp_merge, "凭证货币金额")
merge_2 = pd.concat([merge_2, temp_merge])
columns = [i.strip() for i in merge_2.columns] # 避免模板列索引包含空格,处理相关事项
merge_2.columns = columns
merge = pd.concat([merge_1, merge_2])
del data_special # 不同的循环是否存在跨法人模板情况不一,所以需要额外删除判断变量避免循环时产生逻辑错误
else:
merge = merge_1
merge.loc[merge['贸易伙伴'] == '0', '贸易伙伴'] = None
merge.loc[merge['贸易伙伴'] == 0, '贸易伙伴'] = None
merge['行项目文本'] = merge['行项目文本'].str.replace('XXX公司', company_name)
merge['行项目文本'] = merge['行项目文本'].str.replace('XXX-XXX', setting_deltime)
merge['抬头文本'] = merge['行项目文本']
merge['抬头文本'] = merge['抬头文本'].str.replace(setting_deltime,'')
merge['抬头文本'] = merge['抬头文本'].str.replace('(','')
merge['抬头文本'] = merge['抬头文本'].str.replace('(','')
merge['抬头文本'] = merge['抬头文本'].str.replace(')','')
merge['抬头文本'] = merge['抬头文本'].str.replace(')','')
merge['凭证日期'], merge['过账日期'] = operating_time, operating_time
merge['本位币金额'] = merge['凭证货币金额']
merge['凭证类型'] = 'SA'
merge["货币"] = "CNY"
merge["页数"] = np.nan
merge["汇率"] = np.nan
merge["抬头参照"] = np.nan
merge["税码"] = np.nan
result = merge[merge['公司代码'] == main_code].copy()
# 调整标识(凭证序号)
count = 0
result.iloc[0,0] = 1
for i in range(1, result.shape[0]):
if result.iloc[i, 8] != result.iloc[i-1, 8]:
result.iloc[i, 0] = result.iloc[i-1, 0] + 1
else:
result.iloc[i, 0] = result.iloc[i-1, 0]
with pd.ExcelWriter(os.path.join(save_path, f'{file_name}.xlsx')) as writer:
result.to_excel(writer, sheet_name=bill_num, index=False)
你把pyinstaller删了重下试试看
或者是代码有问题