3

Here is what I have been trying to use:

filename = str(input("Enter a file name: "))
file = filename.split('.')
file = file[0]
from file import *

However, the interpreter doesn't attempt to look for the variable assigned to file, and looks for a module named 'file' does anyone have a solution to this?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Does this answer your question? [import module from string variable](https://stackoverflow.com/questions/8718885/import-module-from-string-variable) – anthony sottile Oct 19 '20 at 04:18

1 Answers1

1

I think that this might work for you:

import importlib
module = importlib.import_module(filename.split('.')[0])

Then you should be able to use module in order to call any of the imported functions.

goodvibration
  • 5,980
  • 4
  • 28
  • 61