This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Cannot change variable defined in "__init__.py" after importing
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: crayor, georg.brandl
Priority: normal Keywords:

Created on 2008-05-20 15:16 by crayor, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg67128 - (view) Author: Jörg Rebenstorf (crayor) Date: 2008-05-20 15:16
How can I change a variable that I defined in "__init__.py" of a 
package called "common" from inside the .py file that imported the 
package? I think there is no way to do this, in contrast to when the 
variable is defined any other module of the same package but not in the 
specific "__init__.py" module.

Example:
If there is a variable named let's say "fileList" in the 
module "var.py" of the package "common" then I could modify the 
variable "fileList" from within the importing file like this:

from common.var import *
var.fileList = [ "bla", "blub" ]

So that the change of the value of "fileList" is seen when reading it 
inside a function of "var.py" after this modification.

But when the variable is defined inside "__init_.py" then the importing 
script cannot access it by writing:

from common import *
fileList = [ "bla", "blub" ]

And it cannot access it by writing:

from common import *
common.fileList = [ "bla", "blub" ]

Why are variables of "__init__.py" module non-modifyable this way and 
all others of submodules of the same package are modifyable? Maybe 
there is this conceptional problem of the python language here or did I 
miss something?
msg67133 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-05-20 15:47
Use

import common
common.fileList = ...

Please ask further question on python-list (comp.lang.python) -- this is
not a bug.
msg67148 - (view) Author: Jörg Rebenstorf (crayor) Date: 2008-05-21 07:32
Oh yes, I am sorry, I forgot to tell you that I *cannot* change the 
import statement as it is part of a framwork by a third-party which I 
cannot influence. This is a prerequisite of my task.

Of course you are right that I am not strictly speaking about a bug but 
rather about a feature request if no appropriate solution exists.

One solution in my situation could be that I could re-import the 
package again using a plain import <package> statement (not a from-
import one as the framework does). Is this the recommended solution or 
shouldn't there be a way to modify the variable even if it is imported 
by a from-import statement?

Maybe you can give me a reason for your opinion?
msg67149 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-05-21 07:35
As I said, please consult the newsgroup/mailing list for these
questions, it's much better suited for them.
History
Date User Action Args
2022-04-11 14:56:34adminsetgithub: 47179
2008-05-21 07:35:22georg.brandlsetmessages: + msg67149
2008-05-21 07:32:44crayorsettype: behavior -> enhancement
messages: + msg67148
2008-05-20 15:47:21georg.brandlsetstatus: open -> closed
resolution: not a bug
messages: + msg67133
nosy: + georg.brandl
2008-05-20 15:24:58crayorsettitle: Cannot change variable defined in __init_.py after importing -> Cannot change variable defined in "__init__.py" after importing
2008-05-20 15:24:27crayorsettitle: Cannot change variable definied in __init_.py after importing -> Cannot change variable defined in __init_.py after importing
2008-05-20 15:16:37crayorcreate