# config.py
white = (255,255,255)
# main.py
import config
print(white)
# output:
Traceback (most recent call last):
File "C:\...\Test\Test2.py", line 2, in <module>
print(white)
NameError: name 'white' is not defined
Process finished with exit code 1
# wanted output
(255, 255, 255)
Process finished with exit code 0
Hello
I want to create a variable in a config.py file, import that file into main.py, and use the variable. But the variable does not become available in main.py. I don't want to change the variable inside main.py, I only want to reference to it. What am I doing wrong?
The code provided is a simplification of the actual code and acts as an example.
One solution I can find is the following. But what shall I do if I have got multiple variables?
# main.py
import config
white_new = config.white
print(white_new)
import config.pywould raise an error. Please always provide a minimal reproducible example But assuming you actually didimport config, then why did you expect to be able toprint(white)? You would need to useconfig.white. Which is probably what you should useconfig.whatever. You could also use afrom config import <list of variable>or evenfrom config import *, but the latter is considered a bad practice. Really, just usingimport configandconfig.white,config.yellowor whatever is the best way