user9060784
user9060784

Reputation: 125

Python, Import config.py

# 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)

Upvotes: 0

Views: 986

Answers (1)

CreepyRaccoon
CreepyRaccoon

Reputation: 867

Just do:

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

For multiple variables:

from config import white, variable_1, variable_2, ...

Upvotes: 1

Related Questions