-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")
lev
  • 1
  • 1
  • 3
    The `==` operator is a comparison, not an assignment. You want just `=`. – Keith Dec 20 '21 at 01:51
  • Also, it is preferred to use the identity operator the booleans: `if config.validated is True:`. – Keith Dec 20 '21 at 01: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? – lev Dec 20 '21 at 01:55
  • 1
    `validated = false` This is not valid python code. Please post the real code. – John Gordon Dec 20 '21 at 01:58
  • @Keith it is preferred not to do the following pattern: `if config.validated is True`, but rather `if config.validated:`. The former is saying, `if True is True`. – d_kennetz Dec 20 '21 at 01:58
  • @lev `False` is a python type boolean. `false` is not. – d_kennetz Dec 20 '21 at 01:58
  • 1
    @d_kennetz that was merely a typo I apologise- on the actual code I have written validated = False. – lev Dec 20 '21 at 02:03
  • @JohnGordon I assure you it is valid python code, it is the only line in the config.py file as that .py file is supposed to be used to regulate the value of validated. again, "false" was simply a typo. – lev Dec 20 '21 at 02:04
  • Nowhere in this code do you set validated to True, so I'm confused why you expect that it would be true. – John Gordon Dec 20 '21 at 02:07
  • @JohnGordon sorry this is my mistake, the first mention of validated is in config.py, in window.py there is a function which determines if a user has inputted a correct password/username, this works fine, if the user inputs correct details it runs the if statement I mentioned at the end. which is meant to set validated to true. I have written "config.validated" to call it from the file config.py – lev Dec 20 '21 at 02:14
  • In the last code block you have the line `config.validated == True`, which does NOT set validated to true (because you used a double equal sign `==` instead of a single equal sign `=`). So, can you show us the code that actually does set validated to true? – John Gordon Dec 20 '21 at 02:16
  • You should never compare against `True` or `False`. Just is `if config.validated:`. The variables are named so that statement reads like what is means: "if config is validated, do this". – Tim Roberts Dec 20 '21 at 02:18
  • @TimRoberts config is a file, I am calling the variable validated from it. config in this instance refers to a file named config.py – lev Dec 20 '21 at 02:24
  • @JohnGordon I have added the edit which attempts to set validated to True – lev Dec 20 '21 at 02:25
  • Doesn't make any difference. "validated" is a boolean thing. Just write `if config.validated:`. – Tim Roberts Dec 20 '21 at 02:25
  • Instead of using a Python file as config, you can try to use a .txt file as config? – khgb Dec 20 '21 at 02:28
  • @TimRoberts i tried, I am still getting no output, the issue seems to me to be that it wont recognise the value of Validated has changed from "false" to "true", or is supposed to – lev Dec 20 '21 at 02:31
  • And also @lev, you can also view this question on SO: https://stackoverflow.com/questions/43861164/passing-data-between-separately-running-python-scripts and see if it helps – khgb Dec 20 '21 at 02:32
  • Please provide a simple but _complete_ [mcve] – Bryan Oakley Dec 20 '21 at 03:54

1 Answers1

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.

khgb
  • 174
  • 1
  • 12
  • _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? – John Gordon Dec 20 '21 at 03:15
  • Apparently, I was wrong. I will edit this post. – khgb Dec 20 '21 at 07:10