Create constants using the settings module?

I am relatively new to Python. I am trying to create a module "settings", which will store various constants for a particular application.

This is how I want to configure my code:

settings.py

CONSTANT = 'value' 

script.py

 import settings def func(): var = CONSTANT # do some more coding return var 

I get a Python error message:

global name 'CONSTANT' is not defined.

I noticed in the Django source code that their settings.py file has constants named just like me. I am confused about how they can be imported into the script and referenced from the application.

EDIT

Thanks for all your answers! I tried the following:

 import settings print settings.CONSTANT 

I get the same error

ImportError: cannot import name CONSTANT

+74
python
Sep 29 '10 at 17:57
source share
9 answers

The easiest way to do this is to simply configure the parameters as a module.

(settings.py)

 CONSTANT1 = "value1" CONSTANT2 = "value2" 

(consumer.py)

 import settings print settings.CONSTANT1 print settings.CONSTANT2 

When importing a python module, you must prefix the variables that you extract from it with the module name. If you know exactly what values ​​you want to use from this file in the file and you do not care about changing them at runtime, then you can do

 from settings import CONSTANT1, CONSTANT2 print CONSTANT1 print CONSTANT2 

but I would not be carried away by this last. This makes it difficult for people reading your code to know where the values ​​come from. and excludes that these values ​​are updated if another client module changes them. The last way to do this is

 import settings as s print s.CONSTANT1 print s.CONSTANT2 

This will allow you to print, distribute updates and require readers to remember that there is something after s from the settings module.

+133
Sep 29 '10 at 17:59
source share

step 1: create a new settings.py file in the same directory for easy access.

 #database configuration settings database = dict( DATABASE = "mysql", USER = "Lark", PASS = "" ) #application predefined constants app = dict( VERSION = 1.0, GITHUB = "{url}" ) 

step 2: import the settings module into the application file.

 import settings as s # s is aliasing settings & settings is the actual file you do not have to add .py print(s.database['DATABASE']) # should output mysql print(s.app['VERSION']) # should output 1.0 

if you don't like using an alias of type s, you can use the syntax

 from settings import database, app print(database['DATABASE']) # should output mysql print(app['VERSION']) # should output 1.0 

notification by the second import method you can directly use dict names

A little tip you can import all the code in the settings file using * if you have a large file and you use most of the settings in your application

 from settings import * # * represent all the code on the file, it will work like step 2 print(database['USER']) # should output lark print(app['VERSION']) # should output 1.0 

Hope this helps.

+16
Jun 03 '13 at 13:46 on
source share

When you import settings , a module object called settings is placed in the global namespace - and this transfer of the object has what was in settings.py as attributes. That is, outside settings.py , you refer to CONSTANT as settings.CONSTANT .

+5
Sep 29 '10 at 18:02
source share

Leave your settings.py as it is, then you can use them just like Django does:

 import settings def func(): var = settings.CONSTANT 
+3
Sep 29 '10 at 17:59
source share

See the answer I posted in Can I prevent the object from being modified in Python? that does what you want (and also force UPPERCASE identifiers). In fact, this may be a better answer to this question than to another.

+3
Sep 30 '10 at 11:29
source share

... Or, if you really want all the constants from settings.py to be imported into the global namespace, you can run

 from settings import * 

... but otherwise, using settings.CONSTANT , as everyone else mentioned here, is perfectly correct.

+2
Sep 29 '10 at 18:13
source share

I am new to python, but if we define a constant as a function on setings.py

 def CONST1(): return "some value" 

main.py

 import setings print setings.CONST1() ##take an constant value 

here I see only one, the value can not be changed, but its work as a function.

+2
Apr 15 '13 at 6:57
source share

Try the following:

In settings.py:

 CONSTANT = 5 

In your main file:

 from settings import CONSTANT class A: b = CONSTANT def printb(self): print self.b 

I think your above error comes from a settings file that is imported too late. Make sure it is at the top of the file.

+1
Sep 29 '10 at 20:47
source share

It is also worth checking out a simple-settings project that allows you to load settings into your script in runtim, which allows for environment settings (I think, dev, test, prod, ...)

0
Aug 14 '17 at 12:22
source share



All Articles