问题
Would it be possible to call a global function from imported function in Python 3?
./folders/folder1/def.py
def do_test():
print("++ def.do_test()")
global_function_1()
print("-- def.do_test()")
./main.py
import importlib
def global_function_1():
print("doing function 1 thing...")
mod = importlib.import_module('folders.folder1.def')
mod.do_test()
I have the error like this.
++ def.do_test()
Traceback (most recent call last):
File "C:\src\python\class2\main.py", line 10, in <module>
mod.do_test()
File "C:\src\python\class2\folders\folder1\def.py", line 4, in do_test
global_function_1()
NameError: name 'global_function_1' is not defined
Apparently, the same functions works just fine if defined in the same file.
def global_function_1():
print("calling the global function 1")
def do_test():
print("++ def.do_test()")
global_function_1()
print("-- def.do_test()")
do_test()
The results is
++ def.do_test()
calling the global function 1
-- def.do_test()
If Python doesn't allow this, what would be the closest alternative ? In my real project, the global functions has number of accesses to the global variables. If all the possible, I'd like to avoid putting all the global functions and variables in a separate class.
EDIT : The above is the code excerpt to highlight my problem. In my real project,
- I have dozen of global functions. So, passing its pointer through function parameter is not preffered.
- I have dozen of other def.py files in multiple folders. I need to pick up the def.py file at run-time depending on variety conditions. So, static reference from main.py to def.py is not preffered.
回答1:
I was able to getmain.py
to work with the following set-up.
(Note I had to add an empty__init__.py
files to both thefolders
andfolder1
subdirectories to get the imports to work.)
File .\class2\folders\folder1\def.py
from main import global_function_1
def do_test():
print("++ def.do_test()")
global_function_1()
print("-- def.do_test()")
File .\class2\main.py
import importlib
def global_function_1():
print("doing function 1 thing...")
if __name__ == '__main__':
mod = importlib.import_module('folders.folder1.def')
mod.do_test()
Output:
++ def.do_test()
doing function 1 thing...
-- def.do_test()
回答2:
What you created is called a module. It can't be used if it's not imported to the file it's used in. In your case, def.py needs to import main.
Importing can be done like this:
from moduleFilename import moduleFunctionname
you can then do
moduleFunctionname()
Alternative:
import moduleFilename
moduleFilename.moduleFunctionname()
EDIT: Seems like Rawing was faster with his comment...
回答3:
If you want to keep folders.folder1.def
independent of back-importing stuff, as it seems like something as a library or plugin or such, you can resort to getting it as a callback:
library:
def do_test(callback):
print("++ def.do_test()")
callback()
print("-- def.do_test()")
main module:
import importlib
def global_function_1():
print("doing function 1 thing...")
mod = importlib.import_module('folders.folder1.def')
mod.do_test(global_function_1)
来源:https://stackoverflow.com/questions/27752913/python-how-to-call-a-global-function-from-a-imported-module