You can place them in __init__.py . This is done all the time.
mypackage/__init__.py
MY_CONSTANT = 42
mypackage/mymodule.py
from mypackage import MY_CONSTANT print "my constant is", MY_CONSTANT
Then import mymodule:
>>> from mypackage import mymodule my constant is 42
However, if you have constants, it would be wise (best, probably) to put them in a separate module (constants.py, config.py, ...), and then if you want them in the package namespace, import them.
mypackage/__init__.py
from mypackage.constants import *
However, this does not automatically include constants in the module namespaces of the package. Each module in the package will still have to import the constants explicitly from either mypackage or mypackage.constants .
Jason R. Coombs Sep 05 '09 at 12:57 2009-09-05 12:57
source share