-1

alright, before marking as duplicate, listen out. I have a file with some .py files. I want to import them, I would normaly do import {the name of the file I want} but I want to chose the name of the file I want like so:

file = input()
import file

what I have tried is:

file = input()
exec('import',file)

which returns me an error,

file = input()
file = importlib.import_module(file)

also returns an error (no module found) and:

file = input()
file = eval(f'import {file}')

which also gives me an error (invalid systax)

rafa_rrayes
  • 155
  • 9

1 Answers1

0

Duplicate of this thread

importlib is recent addition in Python to programmatically import a module. It just a wrapper around __import__ See https://docs.python.org/3/library/importlib.html#module-importlib

import importlib
moduleName = input('Enter module name:')
importlib.import_module(moduleName)

If this does not work for you, look through linked thread at other options.

Community
  • 1
  • 1
APhillips
  • 1,175
  • 9
  • 17