Hi all,
i have different modules developed so far, and a main script needs to call the modules based on user's choice.

will doing like this work??

using raw_input(), i wait for user choice, and once a choice is made, corresponding module is called.

if main.py, does an 'import xxx.py' actually run the script xxx.py or has it to be done with subprocess.call()??

There are 2 slightly different things that you might want to do

  • execute a script xxx.py from your main program. This can be done with exec open("xxx.py").read()
  • import xxx.py as a module, which is done with import xxx or mod = __import__("xxx")

If you import the module, the script xxx.py is only executed the first time the module is imported, and a module object is stored in sys.modules["xxx"] .
If you choose the exec solution, a good idea is to execute the script in a separate dictionary to avoid polluting your namespace, like this

mydict = dict()
exec open("xxx.py").read() in mydict
# or in python 3: exec(open("xxx.py").read(), mydict)

It is unusual to choose a module to import through interaction with the user. A more logical way is that the user chooses an action to execute and this choice results in selecting a function to call. For example

action = raw_input("what should I do ? ").strip()
if action == "go to hell":
    import xxx
elif action == "quit":
    import yyy
# etc
This question has already been answered. Start a new discussion instead.