回答我自己的问题:是的,在运行时(我已经知道)从给定路径/pythonfile导入模块/函数确实有效,即使在PyInstaller中也是如此(这对我来说是新的)。
我将其用于Py2.7程序:
f = r'C:\path\to\userdefined\filewithfunction.py'
if os.path.exists(f):
import imp
userdefined = imp.load_source('', f) # Only Python 2.x, for 3.x see: https://stackoverflow.com/a/67692/701049
print userdefined # just a debugging print
userdefined.imported() # here you should use try/catch; or check whether the function with the desired name really exists in the object "userdefined". This is only a small demo as example how to import, so didnt do it here.
filewithfunction.py:
--------------------
def imported():
print 'yes it worked :-)'
正如在示例代码的注释中所写的那样,在Python 3中需要稍微不同的方法。x、 请参阅此链接:
https://stackoverflow.com/a/67692/701049