I'm trying to import variables from another python files.
a.py - to be imported
text="Hello, world"
print("imported")
b.py - works, but I cannot have static name a
from a import *
print(text)
c.py - not work
import importlib
X = "a"
try:
text = ""
i = importlib.import_module(X)
print(i)
print(text)
except ImportError as err:
print('Error:', err)
try:
text = ""
i = importlib.__import__(X , globals=True, locals=True, fromlist=[], level=0)
print(i)
print(text)
except ImportError as err:
print('Error:', err)
try:
text = ""
i = importlib.__import__(X , globals=True, locals=True, fromlist=[text], level=0)
print(i)
print(text)
except ImportError as err:
print('Error:', err)
ouptut is:
imported
<module 'a' from '/tmp/a.py'>
<module 'a' from '/tmp/a.py'>
<module 'a' from '/tmp/a.py'>
but text varible not working.
d.py - not work
X = "a"
from X import *
print(text)
Is possible to use importlib as from MODULE import *?
Star imports ("from xxx import *") are bad practice (and actively discouraged by pep08) and should only be used as a convenient shortcut in a Python shell for a quick inspection / experimentation / debugging.
So your
b.pycode should either import theamodule and use qualified path ie:or (at worst) explicitely import the desired name(s) from
a:Note that this second solution won't work as expected fior mutable globals, since in that case
textbecomes a module-local name inbso ifa.textis rebound (reassigned) somewhere elseb.textwill not be impacted. But mutable globals are evil anyway...Now assuming you do the RightThing and opt for the first solution (module import + qualified path), the "translation" using
importlibwould be:FWIW, all your attempts at using
importlib.__import__()are wrong -globalsis supposed to be adict, andfromlistshoud be a list of names (strings). But there are very few good reasons for usingimportlib.__import__()instead ofimportlib.import_moduleactually.