Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

Just do:

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

For multiple variables:

from config import white, variable_1, variable_2, ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading