I have two .py files in the same folder: file1.py
and file2.py
.
file1.py
def hello():
print("hello world")
file2.py
from file1 import hello
reload(file1)
hello()
Why do I get the error:
NameError: name 'file1' is not defined
I have two .py files in the same folder: file1.py
and file2.py
.
file1.py
def hello():
print("hello world")
file2.py
from file1 import hello
reload(file1)
hello()
Why do I get the error:
NameError: name 'file1' is not defined
from A import B means that just import B which is in A. Not import A
You import hello() function not file1.
If you want to reload file1, just import file1.
like this:
from imp import reload
import file1
reload(file1)
file1.hello()
Additionally, if you use python 3.X version, you need to add
from imp import reload
or
from importlib import reload
reload()
reloads a previously imported module. See https://docs.python.org/3/library/importlib.html#importlib.reload.
from file1 import hello
only imports hello()
which is a function, not a module.
Also, reload(module)
is available for Python versions <= 3.3. For Python versions >= 3.4, use importlib.reload(module)
.
The proper way is
import importlib
import file1
file1.hello()
importlib.reload(file1)
file1.hello()
I am not an expert but did you try this,
from file1 import hello as q
reload(q)
hello()