python 获取名称错误:将.ipynb转换为.exe文件后未定义名称“null”

6yt4nkrj  于 2023-05-27  发布在  Python
关注(0)|答案(1)|浏览(188)

我正在使用pyinstaller将.ipynb文件转换为.exe文件,因为我希望我的同行成员使用它而不依赖于python,但当我尝试运行exe文件时,我得到以下错误

Traceback(最近一次调用):文件“Bot.ipynb”,第232行,在NameError:名称“null”未定义

这里要考虑的两件事是
1.我没有这个特殊的关键字“null”
1.我的代码总数少于232
下面是我的代码:

from tkinter import *
import PIL
from PIL import ImageTk
from PIL import Image
import tkinter as tk
import webbrowser
from tkinter import scrolledtext
from datetime import datetime

root = tk.Tk()
def update_title():
    current_date_time = datetime.now().strftime("%A,%d-%b-%Y %H:%M:%S")
    root.title(f"Welcome -  {current_date_time}")
    root.after(1000, update_title)
    
update_title()   
root.geometry("1000x700")
root.configure(bg="#D22730")

def open_website(url):
    webbrowser.open(url)

# Function to handle user input and generate bot response
def get_bot_response():
    user_input = input_box.get("1.0", tk.END).strip().lower()
    output_box.configure(state='normal')
    output_box.delete("1.0",tk.END) # Clear previous output
    output_box.insert(tk.END, 'User: {}\n'.format(user_input))
    
    # Check user queries and provide instructions accordingly
     
        
    if 'how to register to ekincare' in user_input or 'ekincare' in user_input or 'medicine' in 
      user_input or 'tele-medicine' in user_input:
        instructions =  "partnered with ekincare, a leading digital healthcare provider 
        to provide you with 24/7 unlimited access to doctor consultation. The coverage is for the 
        employee and upto 5 family members. \n\n" 
        instructions += "The key services provided are: \n"
        instructions += "1.  24/7 unlimited teleconsultation – (General Physician only) via chat or audio \n"
        instructions += "2.  Health Risk Assessment – Track your and your family’s health \n"
        instructions += "3.  COVID Care Management- Home Isolation support for COVID treatment \n"
        instructions += "4.  Discounted Pharmacy –  Up to 20% discount on medicines ordered online (borne by the employee) \n"
        instructions += "5.  Discounted Medical Tests - Up to 40% discount on home sample collection (borne by the employee) \n"
        instructions += "6.  For any queries / support required, please reach out to ekincare at help@ekincare.com or call at +91 49 6816 7274 \n"   
        instructions += "7.  For more details, please refer: https://hrdirect.service-now.com/sys_attachment.do?sys_id=f3c0bd451b37959868314376b04bcbf9 \n"
        

        response = instructions     
        
    elif 'how to add a new domestic beneficiary using mobile banking app' in user_input or 'payee' in user_input or 'add beneficiary' in user_input:
        instructions = "To add new benefiary, please follow the below steps. \n\n" 
        instructions += "1.  Login to your Mobile Banking App \n"
        instructions += "2.  Select 'Move Money' tab \n"
        instructions += "3.  Select 'Pay & Transfer' \n"
        instructions += "4.  Choose the account you’re making the payment from \n"
        instructions += "5.  Under the 'my beneficiaries' section Now you’ll see an option to 'Add a new beneficiary' at the top of your screen \n"
        instructions += "6.  Select and follow the onscreen instructions \n"   
        instructions += "7.  'Confirm' your instruction \n"
        

        response = instructions  
        
    else:
        response = "Sorry, I need to get trained on this topic"
        
    output_box.insert(tk.END, 'Bot: {}\n'.format(response))
    output_box.configure(state='disabled')
    output_box.see(tk.END)
    
input_label = tk.Label(root, text="User Input:",anchor="w",bg="black",fg="white",font=("Arial",12))
input_label.pack(anchor="w")

    
# Input Box
input_box = scrolledtext.ScrolledText(root, height=7, width=100,font=("Arial",12))
input_box.pack()

output_label = tk.Label(root, text="Bot Response:",anchor="w",bg="black",fg="white",font=("Arial",12))
output_label.pack(anchor="w")

# Output
output_box = scrolledtext.ScrolledText(root, height=15,width=100,wrap=tk.WORD,font=("Arial",12))
output_box.pack()    
output_box.configure(borderwidth=1, relief="solid")

#button_font = font.Font(size=12,"bold")
send_button = tk.Button(root, text="Find", command=get_bot_response,bg="black",fg="white",width=8,height=1, font=("Arial",12,"bold"))
send_button.pack()

button_frame = tk.Frame(root)
button_frame.pack(side="bottom",pady=10)
    
button1 = tk.Button(root, text="Service now", relief="flat",width=15, height=1,font=("Arial",10,"bold"),command=lambda: open_website("https://itid.service-now.com/servicenow")) 
button1.pack(side="left",padx=5)

root.mainloop()

wn9m85ua

wn9m85ua1#

这里的问题很可能是由于您试图直接从.ipynb转到可执行文件。在尝试转换之前,您的第一个调用端口应该只是将代码从笔记本中复制/粘贴到普通的.py文件中。
原因是笔记本有一个底层的JSON表示-参见here。可能发生的情况是原始JSON被保存,然后,当您调用文件时,它通过python解释器传递。这解释了两个观察结果:
1.将有一个文字null值,它在JSON中有效,但在Python中无效。如果您使用json模块,它会将它们转换为None,但是这里没有执行反序列化步骤,因为它被视为纯Python对象
1.实际代码的代码行比您预期的要多得多。当所有内容都扩展到我链接的JSON模式时,很容易看到代码可能有一行232,即使它不在IDE显示的表示中。
即使你的.exe * 没有 * 抛出一个错误,我怀疑你所能期望的最好的是一个没有命名的Python字典,它实际上不执行它包含的任何代码。
关于这个here还有其他的答案,但我怀疑直接将错误与潜在问题联系起来并不容易。

相关问题