-2

I am implementing a login system to a program I am writing and have hit a snag- I cannot seem to make a variable retain the same value across all files. I am working in python.

I have made a file named config.py and set the variable to false there

validated = False

in the file main.py I have called config.py and written an if statement-

import window
import config
...
if config.validated == True:
print(config.validated)

finally in the file named window.py, which deals with the login system- I have written a statement to make validated true, the idea being it changes validated in config.py to true which then allows the script to run, the previous part is only set as print (config validated) to test it as the full script was becoming laborious to keep dealing with.

import config
...
if passwordcheck == users[usernamecheck]:
        config.validated == True

the program wont respond, I'm not getting any error messages. it seems as though validated wont change to true across all files, anyone have any idea what I'm missing?

EDIT

 if passwordcheck == users[usernamecheck]:
        config.validated = True
 elif passwordcheck !=users[usernamecheck]:
        tkinter.messagebox.showerror("error", "Password is incorrect")
19
  • 3
    The == operator is a comparison, not an assignment. You want just =. Commented Dec 20, 2021 at 1:51
  • Also, it is preferred to use the identity operator the booleans: if config.validated is True:. Commented Dec 20, 2021 at 1:51
  • fixed these but still getting no response- it's like it just doesn't recognise validated has changed to true or validated isn't changing to true correctly? Commented Dec 20, 2021 at 1:55
  • 1
    validated = false This is not valid python code. Please post the real code. Commented Dec 20, 2021 at 1:58
  • 1
    @d_kennetz that was merely a typo I apologise- on the actual code I have written validated = False. Commented Dec 20, 2021 at 2:03

1 Answer 1

0

You cannot simply import config.py into a script to gather live data as it will create a new instance of script config.py, rather than accessing any variables in the already running script config.py.

To gather live data, you can use an external file, for example JSON or TXT files, and directly write/read to it.

For example, instead of config.py, you can create a file named 'config.txt'. Instead of config.validated=True, you can do:

with open("config.txt", "w") as text_file:
    text_file.write('validated True')
    #note that this will overwrite your config.txt, but if your entire use case is just the validated object, it will be fine.

Instead of directly referencing the config.validated object, you can:

with open('config.txt','r') as config_file:
   validornot = config_file.readlines()[0].split()[1]
   #this will be 'True' or 'False'

Edit: I was wrong, you can import config.py into a script to gather live data. But still, you can use a .txt or .json file.

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

2 Comments

You cannot simply import config.py into a script to gather live data as it will create a new instance of script config.py Not true. If I import config and then assign config.somevariable = 5, that change WILL be visible to other modules within the same execution. Did you actually try this yourself?
Apparently, I was wrong. I will edit this post.

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.