from here I got an idea about how using variables from other modules. this all works fine with
import foo as bar
But I don’t want to import my modules as “bar” I want to use it without any prefix like
from foo import *
Using this it´s impossible to modify variables from other modules. reading will work! any idea? suggestions?
As far as I know, there is no way to import a value from a module and have it readable and writable by the importing scope. When you just
import fooin Python, it creates a module object namedfoo. Getting and setting attributes on a module object will change them in the module’s scope. But when youfrom foo import something,foois imported and a module object is created, but is not returned. Instead, Python copies the values you specified out offooand puts them in the local scope. If what you are importing is an immutable type likeintorstr, then changing it and having the changes reflect in thefoomodule is impossible. It’s similar to this:Excepting crude hacks, if you really want to be able to modify the module’s variable, you will need to import the module itself and modify the variable on the module. But generally, having to do this is indicative of bad design. If you are the writer of the
foomodule in question, you may want to look at some other, more Pythonic ways to solve your problem.