Home > Back-end >  Python code doesn't call class from another program when Import statement is used
Python code doesn't call class from another program when Import statement is used

Time:04-17

I'm trying to write a code a that gets user input via pop up and use that in different program. below is the code which gets user input.

Excel_connection.py

import openpyxl
import tkinter as tk

class App(tk.Frame):
    def __init__(self,master=None,**kw):
        #Create a blank dictionary
        self.answers = {}
        tk.Frame.__init__(self,master=master,**kw)

        tk.Label(self,text="Give Input Sheet Path").grid(row=0,column=0)
        self.Input_From_User1 = tk.Entry(self)
        self.Input_From_User1.grid(row=0,column=1)
        tk.Button(self,text="Feed into Program",command = 
        self.collectAnswers).grid(row=2,column=1)

    def collectAnswers(self):
        self.answers['Input_Path'] = self.Input_From_User1.get()
        global Input_Path
        Input_Path = self.answers['Input_Path']
        functionThatUsesAnswers(self.answers)
        quit()  
    def quit():
    root.destroy()        
if __name__ == '__main__':
    root = tk.Tk()
    App(root).grid()
    root.mainloop()

wb = openpyxl.load_workbook(Input_Path) # trying to open the open the input sheet from the 
below path
ws = wb["Sheet1"]

Below is the code where i'm importing the above program which does some operation

Execution.py

import pandas
from Excel_Connection import *
from Snowflake_Connection import *

all_rows = list(ws.rows)
cur = ctx.cursor()
# Pull information from specific cells.
for row in all_rows[1:400]:
    scenario = row[1].value
    query = row[2].value
    if_execute = row[3].value
    if if_execute == 'Y':
        try:
            cur.execute(query)
            df = cur.fetch_pandas_all()
        except:
            print(scenario," Failed")
        else:
            print("CREATED",scenario,".csv successfully")
print("ALL INDIVDUAL REPORT GENERATED")

When I'm executing Execution.py, the program does not produce pop up window and instead the code throws below error,

wb = openpyxl.load_workbook(Input_Path) # trying to open the open the input sheet from the below path
NameError: name 'Input_Path' is not defined

I tried to -

  1. executing Excel_connection.py separately and it just worked fine.
  2. place the code directly instead of importing the program in the Execution.py and again it worked fine as expected.

The only time I'm facing issue is when I try to import the Excel_connection.py into Excel_connection.py

Could somebody kindly help me out here.

CodePudding user response:

When you import Excel_connection.py, it'll run the code in it. So as you run Execution.py:

  1. import pandas -> "run" the pandas stuff to define functions, etc...
  2. from Excel_connection import * -> You import everything from Excel_connection. So the interpreter will open this file, parse, and run:
    2.1 Class App is defined
    2.2 runs: wb = openpyxl.load_workbook(Input_Path), which is a nonsense. Since as I see Input_Path is defined in App.collectAnswers(), which was never executed before. So there is no Input_Path to use... And you program terminates here, and tells you that.

CodePudding user response:

you can simply use

csv files

or some file format like that to store and transfer data between files.

  • Related