I have a simple program Base.py
that tests if import
is able to throw an exception when the module does not exist.
# Base.py
import threading, os, time
import_is_working = False
class Launch(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.start()
def run(self):
global import_is_working
try:
print "Yes, it got into the 'try' block"
import NON_EXISTENT_MODULE
assert False
except:
print "Great, your python language is working"
import_is_working = True
Launch()
for i in range(500):
time.sleep(0.01)
if import_is_working:
break
if not import_is_working:
print "Your import is not working."
os._exit(4)
Sometimes I like to use this code in another module Main.py
:
# Main.py
import Base
Surprisingly, it doesn't work when I run it that way:
max% python Base.py
Yes, it got into the 'try' block
Great, your python language is working
max% python Main.py
Yes, it got into the 'try' block
Your import is not working.
max%
This happens in Ubuntu, and in a clean CentOS 7.3 install too.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…