4

I am using pyinstaller to convert .ipynb file to .exe file as i want my peers members to use it without depending on python but when i try to run the exe file i get the following error

Traceback (most recent call last): File "Bot.ipynb", line 232, in NameError: name 'null' is not defined

Two things to consider here are

  1. I dont have this particular keyword "null"
  2. my total line of codes are less than 232

Here is my piece of code:

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()

Full traceback

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
Atul sanwal
  • 105
  • 7

1 Answers1

2

The problem here is most likely due to the fact that you're trying to go directly from an .ipynb to an executable. Your first port of call should just be to copy/paste your code out of the notebook and into an ordinary .py file before you try the conversion.

The reason being is that notebooks have an underlying JSON representation - see here. What's likely happening is that the raw JSON is being saved and then, when you call the file, it gets passed through the python interpreter. This explains both observations:

  1. There will be a literal null value, which is valid in JSON but not in Python. If you use the json module, it will convert them to None for you, but that deserialization step isn't happening here because it's being treated as a pure python object
  2. There are way more lines to your actual code than you expect. When everything is expanded into that JSON schema that I linked, it's very simple to see that your code could have a line 232 even though it's not in the representation that your IDE is displaying.

Even if your .exe didn't throw an error, I suspect the best you could hope for is an unnamed python dictionary that doesn't actually execute any of the code it contains.

There are other answers about this here but I suspect it's not so easy to connect your error to the underlying problem directly.

roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • This makes sense, in my case what would be the best way of converting .ipynvb to .py file , simply downloading it as .py file wont work ? – Atul sanwal May 29 '23 at 13:37
  • As I said in the first line of my answer, just copy/paste the code out of the file; it's highly unlikely that your notebook is in any kind of pipeline so there doesn't need to be a programmatic way to do it. I also gave a link to other answers that give different approaches – roganjosh May 29 '23 at 14:54