command line arg?
This is a module named XYZ.
def func(x)
.....
.....
if __name__==开发者_开发技巧"__main__":
print func(sys.argv[1])
Now I have imported this module in another code and want to use the func
. How can i use it?
import XYZ
After this, where to give the argument, and syntax on how to call it, please?
import XYZ
print XYZ.func(foo)
import XYZ
XYZ.func('blah')
or
import XYZ
XYZ.func(sys.argv[1])
The following will bring the name func
into your current namespace so you can use it directly without the module prefix:
from XYZ import func
func(sys.argv[1])
You can also import the module and call the function using its fully qualified name:
import XYZ
XYZ.func(sys.argv[1])
There are a couple of other options as well, for more details read the definitive article on Importing Python Modules
精彩评论