Skip to content Skip to sidebar Skip to footer

How To Import Variables In A Different Python File

I am trying to import some variables from a different python file resides in the same directory from a another python file. been trying this thing for hours and I still couldn't ge

Solution 1:

This is what test.py should look like:

from constimport FOO

print(FOO)

Solution 2:

If you want to get all variable statements as they are you should do:

from const import *

print(FOO)
print(NAMESPACE)

If you want to import variables under module name (file name), you should do:

import const

print(const.FOO)
print(const.NAMESPACE)

If you want to import variables under module name but want to access with constant:

import const as constant

print(constant.FOO)
print(constant.NAMESPACE)

The file name is the module name.

Post a Comment for "How To Import Variables In A Different Python File"