7

I have a package containing many modules. Each module uses constants that I have defined independently in each file. However, all these constants have to be constistent with each other. So I try to define them in a single file and import it in each file. When I run it I have errors for constants not found.

Is their a clean way to have a single file imported by many others and containing constants ?

Thanks for your help

kheraud
  • 5,048
  • 7
  • 46
  • 75
  • 1
    What you described sounds fine in theory. Paste the error, the code around the line where the error happens, the code where you're importing the constants, and the code that's defining the constants. – agf Jul 18 '11 at 10:13
  • how do you import your constants? – Samuele Mattiuzzo Jul 18 '11 at 10:18
  • it was a from XX import * and a XX.constant issue. My goal was to have good practices rather than code debugging – kheraud Jul 18 '11 at 10:40
  • 1
    Good practices would probably be to pass the constants into the constructors for the objects defined in each module. – James Jul 18 '11 at 16:17

1 Answers1

13

You can declare all your constants in one file, say constants.py and then import them into others. Here is an example:

# constants.py
FOO = 'foo'
PI = 3.14

# main.py
import constants
print constants.PI
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 1
    this is the correct answer i guess. Also, you can type "from constants import *" and then you can use your constants without appending "constant." before each call (like in dogbane's example) – Samuele Mattiuzzo Jul 18 '11 at 10:23
  • @dogbane: That sounds EXACTLY like what the OP said he was doing. In the absence of him showing what he really did, we can't help him with his unspecified error. – John Machin Jul 18 '11 at 10:39
  • Yes, the "constant." was the problem origin. The from constant import * solved it ! Thanks – kheraud Jul 18 '11 at 10:42
  • Importing all your constants directly into your namespace works fine for a small app, but it clutters things unnecessarily and you're lying about how the code is structured to anybody who has to maintain it (like Your Future Self). Consider this situation: you make a small change to your code, but in the process you run a function call on a constant that doesn't work on said constant (like running len(int)). If your code just says len(int), you'll go batty trying to find it in that file because that's what namespace you're in. But, if it says len(constants.int), you'll know where to look. – Jonathanb Jul 18 '11 at 12:54
  • `from module import *` is discouraged in general. See [this](http://docs.python.org/howto/doanddont.html#from-module-import). – dogbane Jul 18 '11 at 13:01
  • Thnaks for all these explnations. Thanks for the URL, I will banish the from XX import * now. You are totaly right it would be hard to maintain if all is in the same namespace. – kheraud Jul 18 '11 at 16:21
  • I highly recommend you keep your constants in the constants namespace in the modules where you use need to use them. For one thing, having `constants.WHAT_EVER` is a pretty good way of letting you know they're CONSTANTS. For another, you are less likely to accidentally assign to them. Best, you don't clutter the namespaces of the importing modules. – Michael Kent Jul 18 '11 at 23:39
  • See https://stackoverflow.com/questions/2682745/how-do-i-create-a-constant-in-python. To mark a constant variable as final – A.Casanova Jan 08 '23 at 17:04