1

I'm trying to make a python program for a given software than will scan&run another certain python function at choice (or .py file altogether). However I can't import a function name set in a variable.

import glob

functions_list = glob.glob('*.py')
#all my python files are found
#pick any specific file to import
import a[3]

This fails with SyntaxError: invalid syntax.

Is there any way to do this? My idea is to have a TkInter selection list where I click to run the .py I selected.

Nazim Kerimbekov
  • 4,497
  • 8
  • 31
  • 54
João Mamede
  • 119
  • 10

2 Answers2

1

Why not run the script inside a subprocess using a shell command?

subprocess.call(['python', a[3])

See here for more info: https://www.pythonforbeginners.com/os/subprocess-for-system-administrators

Artur
  • 397
  • 1
  • 8
1

You should have a look at python's imp module. It enables you to dynamically load modules at runtime and then execute their functions.

Léopold Houdin
  • 1,455
  • 12
  • 17