我有一個文件,我正在導入到我的程序(比如說帶有字典的文件)。在這個文件的開頭,我想把一段代碼打印出來,這不是主要文件,然後exit()
。我發現的問題是,此代碼正在導入字典模塊,我不希望發生的導入。如何防止呢?Python:運行一段代碼,除非導入
我試過,但它不工作:
if not Main_file:
print('These aren\'t the droids you\'re looking for')
exit()
在主文件中,當然會有進口前Main_file = True
。
您正在查找:http://stackoverflow.com/questions/419163/what-does-if-name-main-do –