11

I'm new to python, so please excuse what is probably a pretty dumb question.

Basically, I have a single global variable, called _debug, which is used to determine whether or not the script should output debugging information. My problem is, I can't set it in a different python script than the one that uses it.

I have two scripts:

one.py:
-------
def my_function():
 if _debug:
 print "debugging!"
two.py:
-------
from one import *
_debug = False
my_function()

Running two.py generates an error:

NameError: global name '_debug' is not defined

Can anyone tell me what I'm doing wrong?

CharlesB
91.3k29 gold badges203 silver badges228 bronze badges
asked Jan 30, 2009 at 12:56

4 Answers 4

16

There are more problems than just the leading underscore I'm afraid.

When you call my_function(), it still won't have your debug variable in its namespace, unless you import it from two.py.

Of course, doing that means you'll end up with cyclic dependencies (one.py -> two.py -> one.py), and you'll get NameErrors unless you refactor where various things are imported and declared.

One solution would be to create a simple third module which defines 'constants' like this, which can be safely imported from anywhere, e.g.:

constants.py
------------
debug = True
one.py
------
from constants import debug
#...
two.py
------
from constants import debug
#...

However, I would recommend just using the built in logging module for this - why not? It's easy to configure, simpler to use, reliable, flexible and extensible.

answered Jan 30, 2009 at 14:01
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the logging module is the way to go here.
5

Names beginning with an underscore aren't imported with

from one import *
answered Jan 30, 2009 at 13:07

3 Comments

And it is because they mean "this is internal stuff, do not touch it, or do so at your own risk".
I'm actually shocked that a completely incorrect answer has been accepted and voted up so highly.
Well it's not completely incorrect; kgiannakakis is correct about names beginning with underscores not being imported. However, you're correct that this still isn't why his example code doesn't work. Your answer is much better.
4

You can also use the __debug__ variable for debugging. It is true if the interpreter wasn't started with the -O option. The assert statement might be helpful, too.

answered Jan 30, 2009 at 13:57

Comments

1

A bit more explanation: The function my_function's namespace is always in the module one. This means that when the name _debug is not found in my_function, it looks in one, not the namespace from which the function is called. Alabaster's answer provides a good solution.

answered Jan 30, 2009 at 14:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.