You may not know if the file exists and want to use a function in another module without using import.
You can call a fixed function from another module as an add-on. The example below is an example of importing imp.py from machnery_test.py and using test_func ().
machnery_test.py
from importlib import machinery
import os
filename = '{}/imp.py'.format(os.path.dirname(__file__))
loader = machinery.SourceFileLoader('filename', filename)
module = loader.load_module()
print(module.test_func())
同じフォルダ内のimp.py
def test_func():
print('Test function')
return 'Result from test_func'
output
Test function
Result from test_func
Recommended Posts