• 11

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Can I use __init__.py to define global variables?

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

Of course, if this is totally wrong, and there is a better alternative, I'd like to know it.

You should be able to put them in __init__.py. This is done all the time.




from mypackage import MY_CONSTANT
print "my constant is", MY_CONSTANT

Then, import mymodule:

>>> from mypackage import mymodule
my constant is 42

Still, if you do have constants, it would be reasonable (best practices, probably) to put them in a separate module (constants.py, config.py, ...) and then if you want them in the package namespace, import them.


from mypackage.constants import *

Still, this doesn't automatically include the constants in the namespaces of the package modules. Each of the modules in the package will still have to import constants explicitly either from mypackage or from mypackage.constants.

  • 196
Reply Report
      • 1
    • This should have been the accepted answer. If you are working with Python 2.5 or higher you can also use an explicit relative import as well as described here: from . import MY_CONSTANT
      • 2
    • doesn't the second way only work for constants? from mypackage.constants import * will place copies of MY_CONSTANT in every submodule rather than a reference to the same variable
      • 2
    • @hardmooth: Not exactly. The values are copied by reference, so if you were to mutate MY_CONSTANT in any of the modules, it would mutate everywhere. If you were to re-assign MY_CONSTANT in any of the modules, it would only affect that module. If that's your intention, you must reference by attribute, i.e. mypackage.constants.MY_CONSTANT.
    • One catch with the example is if you import mymodule.py in __init__.py before MY_CONSTANT = 42 that will fail because when importing mymodule.py MY_CONSTANT hasn't been defined yet. So need to move MY_CONSTANT = 42 above import mymodule
      • 1
    • how is about variable? It's asking about variables, not constants. How python treats variable inside package? What If variable inside package has been changed?

You cannot do that. You will have to explicitely import your constants into each individual module's namespace. The best way to achieve this is to define your constants in a "config" module and import it everywhere you require it:

# mypackage/config.py

# mypackage/main.py
from mypackage.config import *
  • 31
Reply Report
      • 1
    • Yeah, a configuration file is what I would like. I just thought that init.py would be a good place. Your solution sounds like standard practice. Is it?
      • 2
    • Good point. I didn't realize the question was to have the constants automatically placed in the namespace of all package modules.
      • 2
    • But every time a script imports config.py, the code inside is executed. What do you recommend if the code inside config.py is to be run only once? Say I'm reading a settings.json file inside config.py and I don't want to open() it every time I import config.py.
    • @UGS This is not how Python works. Every module is executed only once. When it is imported the second time, the module is already cached in sys.modules.
      • 1
    • @FerdinandBeyer Oops! I forgot to mention that I'm importing config.py from several scripts and not the same script. Say a.py is importing config.py & b.py and b.py is importing config.py. I was wondering if its possible to make sure code inside config.py is executed only once.

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

And I guess no one would recommend this practice anyway. What's wrong with a namespace? Said application has the version module, so that I have "global" variables available like version.VERSION, version.PACKAGE_NAME etc.

  • 2
Reply Report

Just wanted to add that constants can be employed using a config.ini file and parsed in the script using the configparser library. This way you could have constants for multiple circumstances. For instance if you had parameter constants for two separate url requests just label them like so:

conn = 'admin@localhost'
pass = 'admin'

conn = 'barney@localhost'
pass = 'dinosaur'

I found the documentation on the Python website very helpful. I am not sure if there are any differences between Python 2 and 3 so here are the links to both:

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
Reply Report

Trending Tags