1. as asked by @ndc85430 - why would you import like this in the first place?
2. you are right - it's a scope issue. that's one of the reasons why the recommended way for imports is at the top of the module
2. you are right - it's a scope issue. that's one of the reasons why the recommended way for imports is at the top of the module
def spam(): import os print(os.getcwd()) spam() print(os.getcwd())3. if you really, really, really need to do something like this - don't use
eval
or exec
. use importlib
modueimport importlib def bulk_import(*modules): return (importlib.import_module(some_module) for some_module in modules) os, datetime = bulk_import("os", 'datetime') print(os.getcwd())
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs