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
124k29 gold badges180 silver badges317 bronze badges
asked Jan 17, 2020 at 22:50
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? Commented Jan 17, 2020 at 23:04
  • If I understand, you need import a module named dsajJasndfD? Commented Jan 17, 2020 at 23:05
  • importlib is the module you are looking for docs.python.org/3/library/… Commented Jan 17, 2020 at 23:06
  • stackoverflow.com/questions/3131217/… The second answer there can serve you the solution for your problem. Commented Jan 17, 2020 at 23:06

1 Answer 1

1

This should do the trick:

current_code = "foo.py"
__import__(current_code)
answered Jan 17, 2020 at 23:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.