Home > Net >  Error passing variable from another module
Error passing variable from another module

Time:04-27

I have been trying to pass a variable from one module to another in Python but I get an error.

afile.py

class Window:
    def __init__(self, master, *args):
        .....

    def browse_file(self):
        self.filename = fd.askopenfilename()
        self.textfile.config(text=self.filename)

bfile.py

import pandas as pd
from afile import Window

df = pd.read_html(Window.filename, encoding='utf-8')[0]

what am I doing wrong?

CodePudding user response:

Window is a class, but in your bfile.py it has not be instantiated to create an object.

The .filename object attributes does not exist until after the object has been initialized. (You can have class attributes, but that is not in your code.) You need to first create the object, then access the attribute.

The Window.__init__ method requires an argument called master. You will need to pass in something there to get your object created.

import pandas as pd
from afile import Window

df = pd.read_html(Window(master=???).filename, encoding='utf-8')[0]
  • Related