Can I use __init__.py to define global variables?

I want to define a constant that should be available in all submodules of the package. I thought the best place would be in the __init__.py file of the root package. But I do not know how to do this. Suppose I have several subpackages and each with several modules. How can I access this variable from these modules?

Of course, if this is completely wrong, and there is a better alternative, I would like to know.

+69
python module global-variables packages init
Sep 05 '09 at 12:33
source share
4 answers

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 .

+113
Sep 05 '09 at 12:57
source share

You cannot do this. You will have to explicitly import your constants into each namespace of the individual modules. The best way to achieve this is to define your constants in the "config" module and import it wherever you need it:

 # mypackage/config.py MY_CONST = 17 # mypackage/main.py from mypackage.config import * 
+29
Sep 05 '09 at 12:38
source share

You can define global variables from anywhere, but this is a really bad idea. import the __builtin__ module and change or add attributes to these modules, and suddenly you will have new built-in constants or functions. In fact, when my application installs gettext, I get the _ () function in all my modules without importing anything. Thus, it is possible, but, of course, only for projects such as applications, and not for reusable packages or modules.

And I think no one would recommend this practice anyway. What is wrong with the namespace? The mentioned application has a version module, so I have global variables available as version.VERSION , version.PACKAGE_NAME , etc.

+2
Sep 05 '09 at 15:09
source share

I just wanted to add that the constants can be used using the config.ini file and analyzed in a script using the configparser library. Thus, you can have constants in several circumstances. For example, if you have parameter constants for two separate URL requests, simply name them like this:

 mymodule/config.ini [request0] conn = 'admin@localhost' pass = 'admin' ... [request1] conn = 'barney@localhost' pass = 'dinosaur' ... 

I found the documentation on the Python website very useful. I'm not sure if there are any differences between Python 2 and 3, so the links to them are:

For Python 3: https://docs.python.org/3/library/configparser.html#module-configparser

For Python 2: https://docs.python.org/2/library/configparser.html#module-configparser

0
Jul 08 '16 at 21:41
source share



All Articles