-3

Is it possible to call modules and functions with user input in Python?

For example:

a = input("Module: ")

import a

b = input("Function: ")

a.b()
martineau
  • 119,623
  • 25
  • 170
  • 301
Furkan125
  • 11
  • 4
  • 2
    I think these could be helpful: https://stackoverflow.com/questions/8718885/import-module-from-string-variable and https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string – StardustGogeta Jul 21 '21 at 15:10
  • 1
    Does this answer your question? [How to call Python functions dynamically](https://stackoverflow.com/questions/4246000/how-to-call-python-functions-dynamically) – Abstract Jul 21 '21 at 15:11
  • 3
    Does this answer your question? [import module from string variable](https://stackoverflow.com/questions/8718885/import-module-from-string-variable) – ppwater Jul 21 '21 at 15:11
  • Thanks I solved the problem. The links you posted worked. – Furkan125 Jul 21 '21 at 15:28

1 Answers1

0

you could use

eval(f'import {a}')
eval(f'{a}.{b}')

but I higly recommend you not to just eval random user input.

Gornoka
  • 61
  • 1
  • 5
  • Hello, I solved the problem in different ways, by the way, when I use eval for import, it raises syntax error. – Furkan125 Jul 21 '21 at 15:31