1

I have two different .py file, is it possible to use variables of one in the other?

Sorry for my bad code but I'm still learning :)

In MainFile.py

from LoginFile import login

class MyApp(QtGui.QDialog, Ui_login_dialog):
    def __init__(self, parent=None):
        super(MyApp,self).__init__()
        self.setupUi(self)
        self.login_btn.clicked.connect(self.loginCheck)
        self.Register.clicked.connect(self.registerCheck)
        self.window2 = None
        total_usernames = c.execute("SELECT USERNAME FROM register_table").fetchall()
        for data in total_usernames:
            self.comboBox.addItems(data)

    def login_check(self):
        username = str(self.comboBox.currentText())
        password = str(self.fieldpassword.text())

        login(username, password)

        # I can't read the following variables from the other file
        print(current_status)    
        print(current_username)

In LoginFile.py I have:

 def login(username, password):
     driver=webdriver.Chrome(r"C:\Python27\selenium\webdriver\chromedriver.exe")
     driver.get(site)
     current_username = driver.find_element_by_xpath(".//*[@id='react-root']/section/main/article/header/div[2]/ul/li[1]/span/span").text
     current_status = driver.find_element_by_xpath(".//*[@id='react-root']/section/main/article/header/div[2]/ul/li[2]/a/span").text

Basically I would like to read "current_status" and "current_username" variables in MainFile.py imported from LoginFile.py

I already tried to import everything from the other file, with:

from LoginFile import *

but it doesn't work. I also tried to check answers from similar questions, but I wasn't able to find a solution that work in my case.

Is it possible to do it? is there a better solution to arrive to the same result?

Many thanks in advance!!!

2
  • Have you created another filled called __init__.py in the same directory? Commented Jul 18, 2017 at 16:06
  • Your question isn't clear. Even if you had a function called login in your main.py file, print(current_status) would still fail. login(username, password) just calls the function and throws away any value, so at a minimum, you would need to return something from login and then assign it to something inside login_check. Commented Jul 18, 2017 at 16:13

3 Answers 3

1

The variables you are trying to import are inside a function, so they are out of scope. Even if you called the function they would be destroyed when the function ends unless you return them.The other way is to put them and other logic outside the function and then upon import the logic will be executed and variables assigned, then you would have access to their values. All this is under the assumption that you did not received any errors when the interpreter hit the line where you import.

Sign up to request clarification or add additional context in comments.

1 Comment

Concider acccepting the answer please.
0

from LoginFile import * gives you access to all the functions written in the LoginFile class/module, not its variables. The common workaround way to access the variables in that class is to simply have another function like getVariable() that just returns the variable of interest. Then in the MainFile call that function by LoginFile.getVariable().

Comments

0

define your directory as python directory/ package your directory strurcture look like

/directory 
    __init__.py
    main.py
    LoginFile.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.