问题描述
我导入了一个模块如下:
I imported a module as below:
filename = "email"
mymodule = __import__('actions.'+filename)
我遇到的问题是,文件是立即执行的,而我更愿意从文件中执行特定的功能(这样我可以通过它发送变量)。
the problem I have with this is, that the file is immediatly executing, and I would much rather execute a specific function from the file (that way I can send variables through it).
我基本上使用插件,所以它可以工作。
I am basically working with plugins, so it works.
编辑:暂时是
,我不关心当我添加以下行时脚本是否执行:
Edit:for the time being, I am not concerned with whether or not the script executes when I add the line below:
mymodule = __import__('actions.'+filename)
但我想要工作的是当我在下面添加这一行时,我希望该函数能够执行。但相反,我得到一个错误,模块没有该功能,即使它在脚本中存在。
but what I would like to work is when I add the line below, I would like the function to execute. But instead I get an error that the module dosn't have that function even though it exisits in the script.
mymodule.dosomething(n)
编辑:
我个人不知道认为该函数与它有任何关系,但这里有一个我试图打开的python文件。
Edit:I personally don't think that the function has anything to do with it but here is one python files that I am trying to open.
import webbrowser
def OpenEmail():
handle = webbrowser.get()
handle.open('http://gmail.google.com')
OpenEmail()
print "Your email has been opened"
推荐答案
除非模块执行,否则不存在。你不可能两种方式。也许您需要在模块中添加。
The functions don't exist unless the module executes. You can't have it both ways. Perhaps you need to add a main stanza to the module.
这篇关于python,从模块调用函数的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!