退出 python 中的所有模块
Exit form all modules in python
我正在编写一个代码,其中有各种功能。我为每个特定功能创建了 .py 文件,并在需要时导入它们。示例代码:
# main.py file
import addition
import subtraction
a = input("enter a")
b = input("enter b")
c = input("enter 1 to add 2 to subtract")
if a == 1:
addition.add(a, b) # call to add function in addition module
subtraction.minus(a, b) # call to subtract function in subtraction module
# More code here
# addition.py module
import game # import another self-created module
y = input("do you want to play a game? Enter 1 if yes and 0 if no")
if y == 1:
game.start()
# more similar code
现在,您可以看到我在多个级别的模块内部调用模块。所以我的问题是在我的 game
模块中,如果我使用 exit 命令结束代码,它会结束整个执行还是只结束 game
模块?
当我的代码中出现异常时,我需要一个命令来退出整个代码执行过程。
注意:我不希望 exit 命令在控制台上打印任何内容。正如我之前在另一个项目中使用过 sys.exit() 一次,它会在控制台上打印我不需要的警告,因为该项目是为那些不明白该警告是什么的人准备的。
if I use exit command to end the code, will it end the whole execution
是的,会的(假设你的意思是 sys.exit()
)。
or just the game module
不行,会退出整个程序。
如果您担心 sys.exit()
"prints warnings"(我无法在我的系统上确认 - 该应用程序只是存在并且控制台中没有打印警告)您可以提高 SystemExit
以及您选择的消息:
raise SystemExit("Everything is fine.")
如果你想在程序退出时隐藏警告(这个警告可能是堆栈跟踪,很难从你的问题中猜到)那么你可以将你的代码包装在 try except 块中:
import addition
import subtraction
try:
a = input("enter a")
b = input("enter b")
c = input("enter 1 to add 2 to subtract")
if a == 1:
addition.add(a, b) # call to add function in addition module
subtraction.minus(a, b) # call to subtract function in subtraction module
# ...
except Exception:
pass
请注意,这种技术被认为是非常糟糕的,您可能应该将异常记录到文件中。
用户 sys.exit() 发送的模块中有 10 个使用这个:
# addition.py module
import game # import another self-created module
y = input("do you want to play a game? Enter 1 if yes and 0 if no")
if y == 1:
game.start()
# more similar code
# suppose you want to exit from here
# dont use sys.exit()
# use
raise Exception("Something went wrong")
Finnaly 将异常记录到文件
import addition
import subtraction
import logging
# log to file
logging.basicConfig(filename='exceptions.log',level=logging.DEBUG)
try:
a = input("enter a")
b = input("enter b")
c = input("enter 1 to add 2 to subtract")
if a == 1:
addition.add(a, b) # call to add function in addition module
subtraction.minus(a, b) # call to subtract function in subtraction module
# ...
except Exception as e:
logging.exception(e)
使用此功能,当您的程序意外退出时,您的用户将不会在控制台上看到任何消息。您将能够通过阅读 excetions.log 文件来查看发生了什么异常。
更多信息
我正在编写一个代码,其中有各种功能。我为每个特定功能创建了 .py 文件,并在需要时导入它们。示例代码:
# main.py file
import addition
import subtraction
a = input("enter a")
b = input("enter b")
c = input("enter 1 to add 2 to subtract")
if a == 1:
addition.add(a, b) # call to add function in addition module
subtraction.minus(a, b) # call to subtract function in subtraction module
# More code here
# addition.py module
import game # import another self-created module
y = input("do you want to play a game? Enter 1 if yes and 0 if no")
if y == 1:
game.start()
# more similar code
现在,您可以看到我在多个级别的模块内部调用模块。所以我的问题是在我的 game
模块中,如果我使用 exit 命令结束代码,它会结束整个执行还是只结束 game
模块?
当我的代码中出现异常时,我需要一个命令来退出整个代码执行过程。
注意:我不希望 exit 命令在控制台上打印任何内容。正如我之前在另一个项目中使用过 sys.exit() 一次,它会在控制台上打印我不需要的警告,因为该项目是为那些不明白该警告是什么的人准备的。
if I use exit command to end the code, will it end the whole execution
是的,会的(假设你的意思是 sys.exit()
)。
or just the game module
不行,会退出整个程序。
如果您担心 sys.exit()
"prints warnings"(我无法在我的系统上确认 - 该应用程序只是存在并且控制台中没有打印警告)您可以提高 SystemExit
以及您选择的消息:
raise SystemExit("Everything is fine.")
如果你想在程序退出时隐藏警告(这个警告可能是堆栈跟踪,很难从你的问题中猜到)那么你可以将你的代码包装在 try except 块中:
import addition
import subtraction
try:
a = input("enter a")
b = input("enter b")
c = input("enter 1 to add 2 to subtract")
if a == 1:
addition.add(a, b) # call to add function in addition module
subtraction.minus(a, b) # call to subtract function in subtraction module
# ...
except Exception:
pass
请注意,这种技术被认为是非常糟糕的,您可能应该将异常记录到文件中。
用户 sys.exit() 发送的模块中有 10 个使用这个:
# addition.py module
import game # import another self-created module
y = input("do you want to play a game? Enter 1 if yes and 0 if no")
if y == 1:
game.start()
# more similar code
# suppose you want to exit from here
# dont use sys.exit()
# use
raise Exception("Something went wrong")
Finnaly 将异常记录到文件
import addition
import subtraction
import logging
# log to file
logging.basicConfig(filename='exceptions.log',level=logging.DEBUG)
try:
a = input("enter a")
b = input("enter b")
c = input("enter 1 to add 2 to subtract")
if a == 1:
addition.add(a, b) # call to add function in addition module
subtraction.minus(a, b) # call to subtract function in subtraction module
# ...
except Exception as e:
logging.exception(e)
使用此功能,当您的程序意外退出时,您的用户将不会在控制台上看到任何消息。您将能够通过阅读 excetions.log 文件来查看发生了什么异常。