Python : How to call a global function from a imported module

六月ゝ 毕业季﹏ 提交于 2021-02-17 03:44:09

问题


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,

  1. I have dozen of global functions. So, passing its pointer through function parameter is not preffered.
  2. 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.pyto work with the following set-up.

(Note I had to add an empty__init__.pyfiles to both thefoldersandfolder1subdirectories 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!