0

current_code = dsajJasndfD is in second file also. I need to import it by using local current_code (it will be changing after every program restart).

So here is my question: How can I import module, when I don't know its, but have it name assigned as local variable?

current_code = dsajJasndfD
import database as db
key_list = list(db.current_code.keys())
value_list = list(db.current_code.values())
martineau
  • 119,623
  • 25
  • 170
  • 301
Ignesuss
  • 1
  • 4
  • Shouldn't `dsajJasndfD` be string? Do i get it ok, that it's the name of the other file - the package to be imported? – Dawid Gacek Jan 17 '20 at 23:04
  • If I understand, you need import a module named `dsajJasndfD`? – Gocht Jan 17 '20 at 23:05
  • `importlib` is the module you are looking for https://docs.python.org/3/library/importlib.html#importlib.import_module – Iain Shelvington Jan 17 '20 at 23:06
  • https://stackoverflow.com/questions/3131217/error-handling-when-importing-modules The second answer there can serve you the solution for your problem. – Dawid Gacek Jan 17 '20 at 23:06

1 Answers1

1

This should do the trick:

current_code = "foo.py"
__import__(current_code)
Kumpelinus
  • 640
  • 3
  • 12