Python, Import config.py

Question:

# config.py
white = (255,255,255)

# main.py
import config
print(white)

# output:
Traceback (most recent call last):
  File "C:...TestTest2.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)
Asked By: user9060784

||

Answers:

Just do:

# main.py
from config import white
print(white)

For multiple variables:

from config import white, variable_1, variable_2, ...
Answered By: CreepyRaccoon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.