How to import variable from another file in Python

Posted on: January 20th, 2023
By: Tadeo Martinez

from filename import variable_name

No need to add the .py extension when adding the filename.

If you’d like to import everything from that file you can do

from filename import *

However, this isn’t best practice as you might get a warning saying pyflake isn’t sure if the variable name is imported or not.

You can also import the file and add the file name when using that variable.

import filename

print(filename.variable_name)

Have any questions or comments? Write them below!


Leave a Reply

Your email address will not be published. Required fields are marked *