0

I need to do something like that:

a = input('Import the package: ')
import a

Problem: a is a string. I do not know how to do it. The best idea I have is to do:

a = input('Import the package: ')
if a == 'numpy':
    import numpy

But if I need to import many packages, this code can be difficult to read. a = 'numpy' is an example. I need this because I am writing a program that needs to import different scripts in different situations.

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

1

You can use importlib.import_module() function. (docs)

import importlib

your_string = 'numpy'

i = importlib.import_module(your_string)
Rithin Chalumuri
  • 1,739
  • 7
  • 19