Reputation: 33
I'm writing a python package and am wondering where the best place is to put constants?
I know you can create a file called 'constants.py' in the package and then call them with module.constants.const, but shouldn't there be a way to associate the constant with the whole module? e.g. you can call numpy.pi, how would I do something like that?
Also, where in the module is the best place to put paths to directories outside of the module where I want to read/write files?
Upvotes: 3
Views: 6404
Reputation: 1125058
Put them where you feel they can most easily be maintained. Usually that means in the module to which the constants logically belong.
You can always import the constants into the __init__.py
file of your package to make it easier for someone to find them. If you did decide on a constants
module, I'd add a __all__
sequence to state what values are public, then in the __init__.py
file do:
from constants import *
to make the same names available at the package level.
Upvotes: 7