鱼C论坛

 找回密码
 立即注册
查看: 1066|回复: 5

[已解决]pyinstaller

[复制链接]
发表于 2020-8-3 23:17:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
为什么用pyinstaller -D -w Main.py打包完成后,运行dist里面的Main.exe会弹窗Failed to execute script main
图片是打包的过程,下面是代码,请帮忙看看问题出在哪
  1. from tkinter import *
  2. from tkinter.messagebox import *


  3. class Client:
  4.     def __init__(self, root, username):
  5.         self.username = username
  6.         self.root = root
  7.         self.root.title('客户信息-查询系统')
  8.         self.root.minsize(960, 560)
  9.         self.root.maxsize(960, 560)
  10.         self.root.geometry('960x560+360-180')
  11.         self.createpage()
  12.         clients_info = self.find()
  13.         self.clear_text()
  14.         new_clients = []
  15.         information = ''
  16.         for clients in clients_info:
  17.             if clients != '\n':
  18.                 new_clients.append(clients)
  19.         for new_client in new_clients:
  20.             information = '客户姓名:' + new_client.split(',')[0] + '\t' + '客户电话:' + new_client.split(',')[
  21.                 1] + '\t' + '客户证件号码:' + new_client.split(',')[2] + '\t' + '销售员:' + \
  22.                           new_client.split(',')[3]
  23.             self.text.insert(END, information)

  24.     def createpage(self):
  25.         self.frame = Frame(self.root)
  26.         self.frame.pack(fill=BOTH)

  27.         Label(self.frame, text='通过姓名查询:').grid(row=0, column=0, padx=5, pady=5)
  28.         self.name = StringVar()
  29.         Entry(self.frame, textvariable=self.name).grid(row=0, column=1, padx=5, pady=5)
  30.         Button(self.frame, text='查询', command=self.namefind).grid(row=0, column=2, padx=5, pady=5)

  31.         Label(self.frame, text='通过电话查询:').grid(row=0, column=3, padx=5, pady=5)
  32.         self.phone = StringVar()
  33.         Entry(self.frame, textvariable=self.phone).grid(row=0, column=4, padx=5, pady=5)
  34.         Button(self.frame, text='查询', command=self.phonefind).grid(row=0, column=5, padx=5, pady=5)

  35.         Label(self.frame, text='通过证件查询:').grid(row=0, column=6, padx=5, pady=5)
  36.         self.id_nub = StringVar()
  37.         Entry(self.frame, textvariable=self.id_nub).grid(row=0, column=7, padx=5, pady=5)
  38.         Button(self.frame, text='查询', command=self.idfind).grid(row=0, column=8, padx=5, pady=5)

  39.         self.text = Text(self.frame)
  40.         self.text.grid(row=1, column=0, columnspan=9, ipadx=192, ipady=80, padx=5, pady=5,
  41.                        sticky=NSEW)

  42.     def find(self):
  43.         with open(r'clients/client.txt', encoding='UTF-8')as client_info:
  44.             clients = client_info.readlines()
  45.             return clients

  46.     def namefind(self):
  47.         new_name = self.name.get().strip()
  48.         self.clear_text()
  49.         self.clear_entry()
  50.         if new_name:
  51.             clients = self.find()
  52.             new_clients = []
  53.             names = []
  54.             for client in clients:
  55.                 if client != '\n':
  56.                     new_clients.append(client)
  57.                     names.append(client.split(',')[0])
  58.             if new_name in names:
  59.                 information = ''
  60.                 for new_client in new_clients:
  61.                     if new_name == new_client.split(',')[0]:
  62.                         information += '客户姓名:' + new_client.split(',')[0] + '\t' + '客户电话:' + new_client.split(',')[
  63.                             1] + '\t' + '客户证件号码:' + new_client.split(',')[2] + '\t' + '销售员:' + \
  64.                                        new_client.split(',')[3]
  65.                         self.text.insert(END, information)
  66.                     information = ''
  67.             else:
  68.                 showinfo(title='错误', message='未录入此客户!')
  69.         else:
  70.             showinfo(title='错误', message='查询索引不能空!')

  71.     def phonefind(self):
  72.         new_phone = self.phone.get().strip()
  73.         self.clear_text()
  74.         self.clear_entry()
  75.         if new_phone:
  76.             if len(new_phone) == 11:
  77.                 clients = self.find()
  78.                 new_clients = []
  79.                 phones = []
  80.                 for client in clients:
  81.                     if client != '\n':
  82.                         new_clients.append(client)
  83.                         phones.append(client.split(',')[1])
  84.                 if new_phone in phones:
  85.                     information = ''
  86.                     for new_client in new_clients:
  87.                         if new_phone == new_client.split(',')[1]:
  88.                             information += '客户姓名:' + new_client.split(',')[0] + '\t' + '客户电话:' + new_client.split(',')[
  89.                                 1] + '\t' + '客户证件号码:' + new_client.split(',')[2] + '\t' + '销售员:' + \
  90.                                            new_client.split(',')[3]
  91.                             self.text.insert(END, information)
  92.                         information = ''
  93.                 else:
  94.                     showinfo(title='错误', message='未录入此号码!')
  95.             else:
  96.                 showinfo(title='错误', message='输入号码错误!')
  97.         else:
  98.             showinfo(title='错误', message='查询索引不能空!')

  99.     def idfind(self):
  100.         new_id = self.id_nub.get().strip()
  101.         self.clear_text()
  102.         self.clear_entry()
  103.         if new_id:
  104.             if len(new_id) == 18 or new_id == '未录入':
  105.                 clients = self.find()
  106.                 new_clients = []
  107.                 ids = []
  108.                 for client in clients:
  109.                     if client != '\n':
  110.                         new_clients.append(client)
  111.                         ids.append(client.split(',')[2])
  112.                 if new_id in ids:
  113.                     information = ''
  114.                     for new_client in new_clients:
  115.                         if new_id == new_client.split(',')[2]:
  116.                             information += '客户姓名:' + new_client.split(',')[0] + '\t' + '客户电话:' + new_client.split(',')[
  117.                                 1] + '\t' + '客户证件号码:' + new_client.split(',')[2] + '\t' + '销售员:' + \
  118.                                            new_client.split(',')[3]
  119.                             self.text.insert(END, information)
  120.                         information = ''
  121.                 else:
  122.                     showinfo(title='错误', message='未录入此证件号!')
  123.             else:
  124.                 showinfo(title='错误', message='输入证件号错误!')
  125.         else:
  126.             showinfo(title='错误', message='查询索引不能空!')

  127.     def clear_text(self):
  128.         self.text.delete(0.0, END)

  129.     def clear_entry(self):
  130.         self.name.set('')
  131.         self.phone.set('')
  132.         self.id_nub.set('')
复制代码
  1. from tkinter import *
  2. from tkinter.messagebox import *
  3. from Client import *


  4. class EnterPage:
  5.     def __init__(self, mainpage, username):
  6.         self.root = mainpage
  7.         self.username = username
  8.         self.root.title('客户信息-录入系统')
  9.         self.root.minsize(560, 256)
  10.         self.root.maxsize(560, 256)
  11.         self.root.geometry('560x256+480-380')
  12.         self.createpage()

  13.     def createpage(self):
  14.         self.frame = Frame(self.root)
  15.         self.frame.pack()

  16.         Label(self.frame).grid(row=0, rowspan=2)
  17.         Label(self.frame, text='客户姓名:', font=('幼圆', 11)).grid(row=4, column=0, pady=10, sticky=E)
  18.         self.client_name = StringVar()
  19.         Entry(self.frame, textvariable=self.client_name).grid(row=4, column=1, columnspan=3, ipadx=20)
  20.         Label(self.frame, text='*', font=('幼圆', 10), fg='red').grid(row=4, column=4, pady=10, sticky=E)
  21.         Label(self.frame, text='客户电话:', font=('幼圆', 11)).grid(row=5, column=0, pady=10, sticky=E)
  22.         self.client_phone = StringVar()
  23.         Entry(self.frame, textvariable=self.client_phone).grid(row=5, column=1, columnspan=3, ipadx=20)
  24.         Label(self.frame, text='*', font=('幼圆', 10), fg='red').grid(row=5, column=4, pady=10, sticky=E)
  25.         Label(self.frame, text='客户证件号:', font=('幼圆', 11)).grid(row=6, column=0, pady=10, sticky=E)
  26.         self.client_id = StringVar()
  27.         Entry(self.frame, textvariable=self.client_id).grid(row=6, column=1, columnspan=3, ipadx=20)
  28.         Label(self.frame, text='销售员:', font=('幼圆', 11)).grid(row=7, column=0, pady=10, sticky=E)
  29.         self.salesman = StringVar()
  30.         Entry(self.frame, textvariable=self.salesman, state='readonly').grid(row=7, column=1, columnspan=3, ipadx=20)
  31.         self.salesman.set(self.username)
  32.         Button(self.frame, text='保存', command=self.save).grid(row=8, column=1, pady=10, ipadx=10)
  33.         Button(self.frame, text='查询', command=self.find).grid(row=8, column=2, pady=10, ipadx=10)
  34.         Button(self.frame, text='退出', command=quit).grid(row=8, column=3, pady=10, ipadx=10)

  35.     def save(self):
  36.         if not (self.client_name.get() and self.client_phone.get()):
  37.             if not self.client_name.get() and self.client_phone.get():
  38.                 showinfo(title='错误', message='客户姓名为必填项!')
  39.             elif self.client_name.get() and not self.client_phone.get():
  40.                 showinfo(title='错误', message='客户电话为必填项!')
  41.             else:
  42.                 showinfo(title='错误', message='*号为必填项!')
  43.         else:
  44.             if self.client_phone.get().isdigit() and len(self.client_phone.get()) == 11:
  45.                 client_id = self.client_id.get()
  46.                 if not client_id:
  47.                     client_id = '未录入'
  48.                     with open(r'clients/client.txt', encoding='UTF-8')as client_info:
  49.                         clients = client_info.readlines()
  50.                         for client in clients:
  51.                             if client != '\n':
  52.                                 if client.split(',')[1] == self.client_phone.get():
  53.                                     salesman = re.search(r'.*', client.split(',')[3]).group()
  54.                                     showinfo(title='错误',
  55.                                              message='手机号为"{}"的客户已存在,登记姓名为"{}",录入人员"{}"!'.format(
  56.                                                  self.client_phone.get(),
  57.                                                  client.split(',')[0],
  58.                                                  salesman))
  59.                                     self.clear_entry()
  60.                                     break
  61.                         else:
  62.                             with open(r'clients/client.txt', 'a', encoding='UTF-8')as client:
  63.                                 client.write(
  64.                                     '{},{},{},{}\n'.format(self.client_name.get(), self.client_phone.get(),
  65.                                                            client_id, self.salesman.get()))
  66.                                 showinfo(title='提示', message='保存成功!')
  67.                                 self.clear_entry()

  68.                 else:
  69.                     if len(self.client_id.get()) == 18:
  70.                         with open(r'clients/client.txt', encoding='UTF-8')as client_info:
  71.                             clients = client_info.readlines()
  72.                             for client in clients:
  73.                                 if client != '\n':
  74.                                     salesman = re.search(r'.*', client.split(',')[3]).group()
  75.                                     if client.split(',')[1] == self.client_phone.get() or client.split(',')[
  76.                                         2] == self.client_id.get():
  77.                                         if client.split(',')[1] == self.client_phone.get() and client.split(',')[
  78.                                             2] == self.client_id.get():
  79.                                             showinfo(title='错误',
  80.                                                      message='手机号为"{}",证件号为"{}"的客户已存在,登记姓名为"{}",录入人员"{}"!'.format(
  81.                                                          self.client_phone.get(), self.client_id.get(),
  82.                                                          client.split(',')[0],
  83.                                                          salesman))
  84.                                             self.clear_entry()
  85.                                             break
  86.                                         elif client.split(',')[1] == self.client_phone.get() and client.split(',')[
  87.                                             2] != self.client_id.get():
  88.                                             showinfo(title='错误',
  89.                                                      message='手机号为"{}"的客户已存在,登记姓名为"{}",录入人员"{}"!'.format(
  90.                                                          self.client_phone.get(), client.split(',')[0], salesman))
  91.                                             self.clear_entry()
  92.                                             break
  93.                                         elif client.split(',')[1] != self.client_phone.get() and client.split(',')[
  94.                                             2] == self.client_id.get():
  95.                                             showinfo(title='错误',
  96.                                                      message='证件号为"{}"的客户已存在,登记姓名为"{}",录入人员"{}"!'.format(
  97.                                                          self.client_id.get(), client.split(',')[0], salesman))
  98.                                             self.clear_entry()
  99.                                             break

  100.                             else:
  101.                                 with open(r'clients/client.txt', 'a', encoding='UTF-8')as client:
  102.                                     client.write(
  103.                                         '{},{},{},{}\n'.format(self.client_name.get(), self.client_phone.get(),
  104.                                                                client_id, self.salesman.get()))
  105.                                     showinfo(title='提示', message='保存成功!')
  106.                                     self.clear_entry()
  107.                     else:
  108.                         showinfo(title='错误', message='证件号码错误!')
  109.             else:
  110.                 showinfo(title='错误', message='电话号码错误!')
  111.                 self.client_phone.set('')

  112.     def find(self):
  113.         self.frame.destroy()
  114.         Client(self.root, self.username)

  115.     def clear_entry(self):
  116.         self.client_name.set('')
  117.         self.client_phone.set('')
  118.         self.client_id.set('')
复制代码
  1. from tkinter import *
  2. from tkinter.messagebox import *
  3. import re
  4. from EnterPage import *


  5. class LoginPage:
  6.     def __init__(self, loginroot):
  7.         self.root = loginroot
  8.         self.root.title('登录')
  9.         self.root.minsize(360, 180)
  10.         self.root.maxsize(360, 180)
  11.         self.root.geometry('360x180+550-350')
  12.         self.createpage()

  13.     def createpage(self):
  14.         self.frame = Frame(self.root)
  15.         self.frame.pack()
  16.         Label(self.frame, text='用户名:', font=('幼圆', 11)).grid(row=0, sticky=W)
  17.         self.username = StringVar()
  18.         Entry(self.frame, textvariable=self.username).grid(row=0, column=1, columnspan=3, pady=20, ipadx=30, sticky=W)
  19.         Label(self.frame, text='密码:', font=('幼圆', 11)).grid(row=1, stick=W, pady=10)
  20.         self.password = StringVar()
  21.         Entry(self.frame, textvariable=self.password, show='*').grid(row=1, column=1, columnspan=3, pady=10, ipadx=30,
  22.                                                                      sticky=W)
  23.         Button(self.frame, text='登录', command=self.logincheck).grid(row=2, column=1, ipadx=10)
  24.         Button(self.frame, text='注册', command=self.register).grid(row=2, column=2, ipadx=10)
  25.         Button(self.frame, text='取消', command=quit).grid(row=2, column=3, ipadx=10)

  26.     def logincheck(self):
  27.         with open(r'users/users.txt', encoding='UTF-8')as users_name_password:
  28.             users = users_name_password.readlines()
  29.             if users:
  30.                 name = []
  31.                 for user in users:
  32.                     name.append(user.split(',')[0])
  33.                 if self.username.get() and self.password.get():
  34.                     if self.username.get() in name:
  35.                         for user in users:
  36.                             if self.username.get() == user.split(',')[0]:
  37.                                 password = re.search(r'.*', user.split(',')[1]).group()
  38.                                 if self.password.get() == password:
  39.                                     self.clear_entry()
  40.                                     self.frame.destroy()
  41.                                     EnterPage(self.root, user.split(',')[0])
  42.                                     break
  43.                                 else:
  44.                                     self.clear_entry()
  45.                                     showinfo(title='错误', message='用户名或密码不正确!')
  46.                                     break
  47.                     else:
  48.                         self.name_notfound()
  49.                 else:
  50.                     showinfo(title='错误', message='用户名或密码不能为空!')
  51.             else:
  52.                 self.name_notfound()

  53.     def register(self):
  54.         if not (self.username.get() and self.password.get()):
  55.             showinfo(title='错误', message='用户名或密码不能为空!')
  56.         else:
  57.             with open(r'users/users.txt', encoding='UTF-8')as users_name_password:
  58.                 users = users_name_password.readlines()
  59.                 if users:
  60.                     for user in users:
  61.                         username = user.split(',')[0]
  62.                         if self.username.get() == username:
  63.                             showinfo(title='提示', message='用户名"{}"已存在!'.format(username))
  64.                             self.clear_entry()
  65.                             break
  66.                     else:
  67.                         self.write_file()
  68.                 else:
  69.                     self.write_file()

  70.     def write_file(self):
  71.         with open(r'users/users.txt', 'a', encoding='UTF-8')as users:
  72.             users.write('{},{}\n'.format(self.username.get(), self.password.get()))
  73.         self.clear_entry()
  74.         showinfo(title='提示', message='注册成功!')

  75.     def name_notfound(self):
  76.         showinfo(title='错误', message='用户名"{}"不存在!'.format(self.username.get()))
  77.         self.clear_entry()

  78.     def clear_entry(self):
  79.         self.username.set('')
  80.         self.password.set('')
复制代码
  1. from tkinter import *
  2. from LoginPage import *


  3. if __name__ == '__main__':
  4.     root = Tk()
  5.     LoginPage(root)
  6.     root.mainloop()
复制代码
最佳答案
2020-8-8 22:09:33
不用看代码, 目测是项目路径导致的, 由于Pyinstaller在打包时, 项目路径如果不指定可能会被偏移到/temp路径下, 所以在import的时候就找不到对应的模块了
所以对于打包程序, 项目中不能使用相对路径, 而且BASE_DIR需要进行绑定, 推荐在入口文件中增加:
  1. import os
  2. import sys

  3. BASE_DIR = os.path.dirname(sys.argv[0])
  4. sys.path.insert(0, BASE_DIR)
  5. print(BASE_DIR)
复制代码

项目中相对路径应该引用BASE_DIR调整为绝对路径, 否则生成文件你将会找不到在哪
1.png
2.png
3.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-3 23:34:28 | 显示全部楼层
试试打包:
  1. pyinstaller -F -w Main.py
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-4 00:19:03 | 显示全部楼层
控制台程序生成EXE命令:
  1. pyinstaller -F 文件名.py
复制代码
窗口类程序生成EXE命令:
  1. pyinstaller -Fw 文件名.py
复制代码

否则就会出现你现在的情况。


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-8 22:09:33 | 显示全部楼层    本楼为最佳答案   
不用看代码, 目测是项目路径导致的, 由于Pyinstaller在打包时, 项目路径如果不指定可能会被偏移到/temp路径下, 所以在import的时候就找不到对应的模块了
所以对于打包程序, 项目中不能使用相对路径, 而且BASE_DIR需要进行绑定, 推荐在入口文件中增加:
  1. import os
  2. import sys

  3. BASE_DIR = os.path.dirname(sys.argv[0])
  4. sys.path.insert(0, BASE_DIR)
  5. print(BASE_DIR)
复制代码

项目中相对路径应该引用BASE_DIR调整为绝对路径, 否则生成文件你将会找不到在哪
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-10 10:42:29 | 显示全部楼层
BSOD 发表于 2020-8-8 22:09
不用看代码, 目测是项目路径导致的, 由于Pyinstaller在打包时, 项目路径如果不指定可能会被偏移到/temp路径 ...

貌似不需要这么麻烦,放到lite-packages里面不就行了吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-10 12:33:30 From FishC Mobile | 显示全部楼层
代码相对路径不对,将打包后的exe文件放到main.py的那个文件夹下即可运行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-25 04:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表