-2

i have 3 python scripts ('testPrint01.py','testPrint02.py','testPrint03.py') and i would like to call function from 'testPrint02'

    import sys
    sys.path.append(path)

    a = ['testPrint01','testPrint02','testPrint03']
    import sys.a[1]
    a[1].justPrintIt()

thank you

Angga
  • 15
  • 6

1 Answers1

0

If you're in the same directory as your target script you can use

from testPrint02 import x

Where x is the function you want to import.

Edit: To import a module from a string variable you can use importlib as described here: import module from string variable

Community
  • 1
  • 1
imant
  • 597
  • 5
  • 15
  • Hi imant, thank you for the answer, but what i wanna ask is how to import/ call another script from the string, because i try to create a script to call all my script (choose one of the script from the list, and click button to run it) – Angga Oct 02 '16 at 11:54
  • For this you might want to use importlib: `from importlib import import_module`, `i = import_module(a[1])`. You can then use `i.justPrintIt()`. – imant Oct 02 '16 at 12:06
  • oh thank you imant, its working :D – Angga Oct 02 '16 at 12:18