Jorge Creus
Jorge Creus

Reputation: 17

Import variables from a config File

I have a script that have multiple files and a config file with all the variables store in one Python file.

Folder structure:

Folder structure

Config file:

Config file

If I try to run the main file which calls the head function imported, an error pops up saying that the config cannot be imported.

Imports:

Imports image

Upvotes: 1

Views: 4063

Answers (2)

Alexandre Mahdhaoui
Alexandre Mahdhaoui

Reputation: 841

Your Functions folder has a __init__.py file. If your app executes from Main.py (ie if Main.py satisfies __name__ == "__main__") therefore wherever you are in your app you could import the config like this:

from Functions.Config import *

Edit:

However,from module import * is not recommended with local import. You could give an alias to the import and call name.variable instead of calling name directly. Example:

def head():
    # import packages
    import Function.Config as conf
    print(conf.more_200)
head()
>>> 50

Upvotes: 1

pxDav
pxDav

Reputation: 1814

Your syntax are wrong.

It is supposed to be from Config import * and not import .Config import *

Upvotes: 0

Related Questions