views:

80

answers:

3

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?

+4  A: 
import XYZ
print XYZ.func(foo)
stanlekub
+2  A: 
import XYZ
XYZ.func('blah')

or

import XYZ
XYZ.func(sys.argv[1])
djc
+1  A: 

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

Tendayi Mawushe