0

I would like to use a variable that I define in my application inside of a module.

The folder structure:

myapp.py

modules/checkargs.py

modules/init.py (an empty file)

Main app (myapp.py):

_PARAMETERS = {
    'stuff': 'here'
}

from modules.checkargs import checkargs

if __name__ == "__main__":
    checkargs(sys.argv[1:])

checkargs.py:

def checkargs(argv):

    global _PARAMETERS;

    #more Python insanity here

The error:

NameError: global name '_PARAMETERS' is not defined

2 Answers 2

3

In general, you should avoid this style of programming. Modules shouldn't rely on global variables defined in other modules. A better solution would be to pass _PARAMETERS in to checkargs, or move _PARAMETERS to a file that can be shared by multiple modules.

Passing the data to checkargs

Generally speaking, relying on global variables is a bad idea. Perhaps the best solution is to pass PARAMETERS directly into your checkargs function.

# checkargs.py
def checkargs(argv, parameters):
    ...

# myapp.py
if __name__ == "__main__":
    checkargs(sys.argv[1:], _PARAMETERS)

Creating a shared data module

If you have data that you want to share between modules, you can place that data in a third module that every other module imports:

# modules/shared.py
PARAMETERS = {...}

# myapp.py
from modules.shared import PARAMETERS

# checkargs.py
from modules.shared import PARAMETERS

Other solutions

There are other solutions, though I think the above two solutions are best. For example, your main program can copy the parameters to the checkargs module like this:

# myapp.py
import checkargs
checkargs._PARAMETERS = _PARAMETERS
...

You could also have checkargs directly reference the value in your main module, but that requires a circular import. Circular imports should be avoided.

Sign up to request clarification or add additional context in comments.

Comments

0

Why would it be defined? It's from a different module. Inside checkargs.py, you'd need to do:

from myapp import _PARAMETERS

However:

  1. You shouldn't name it with a _ then, since that implies private/protected variables.
  2. You should probably pass the dictionary from myapp into the checkargs functions instead of importing it there. If you don't, you're creating a circular import, which is logically terrible and doesn't actually work.

3 Comments

This gives you circular dependencies, which should be avoided - myapp depends on checkargs, and checkargs depends on myapp
Yes, I see that you address that issue. However, your first statement says "you need to do". "Need" is a very strong word here. In fact, the OP absolutely does not need to do that. They can do it, but it's a bad idea. Maybe that's splitting a fine hair, but it's human nature to see an expert say "you need to do X" and so you go off and do it.
I softened that statement a bit... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.