I run the following programs. It is important to note that there is a file in the directory where both of these programs are located mymodule.py.
First:
exec('''import sys
import os
os.chdir('/')
sys.path = []
import mymodule''', {})
The second:
import mymodule
exec('''import sys
import os
os.chdir('/')
sys.path = []
import mymodule''', {})
The first snippet picks up ImportError, as expected (in the end, the directory where mymodule is in is not in the path). The second snippet, however, does not, although mymodule is also not in its way, and the environment I am giving is empty.
My question is why
source
share