如何 运行 不同的代码取决于 python 脚本从终端或 IDE 中 运行 的方式?
How to run different code depends on the way python script is runned from the terminal or in an IDE?
好的,假设我有这部分代码(它并不完美)。我想要这种情况 - 我正在检测云名称作为 __init__
中的参数,因此所有其他模块和脚本都将 运行 在该云上,或者如果我想 运行 特定 python 来自终端的文件 我可以检测到我希望它在其中 运行ned 的云 python my_script.py cloud1
执行此操作的最佳方法是什么?
当我使用参数从终端 运行 宁它时,以下脚本确实有效,但如果不是,它会给出此错误
usage: To check what is the cloud name config_parser.py: error: too few arguments'
这是一个代码
class CredentialsCP:
def __init__(self, cloud_name=None):
self.config = ConfigParser.ConfigParser()
self.cloud_name = cloud_name
self.config_file_pass = os.path.expanduser('~/PycharmProjects/ui/config.cfg')
self.parser = ArgumentParser(usage='To check what is the cloud name')
self.parser.add_argument('cloud')
self.args = self.parser.parse_args()
if self.args:
self.cloud_name = self.args.cloud
if self.cloud_name is None:
self.cloud_name = 'cloud1'
我有一个显示云的 url 的函数,它是如何调用的
ArgumentParser
提供了可选参数,但位置(非标志)参数的默认设置是 必需的 。在这里你可以使用:
self.parser = ArgumentParser(usage='To check what is the cloud name')
self.parser.add_argument('cloud', nargs='?', default='cloud1') # optional argument with default value
self.args = self.parser.parse_args()
self.cloud_name = self.args.cloud
可能的改进:
在这段代码中,parser
和args
是class的成员属性。如果它们不在 __init__
方法之外使用,它们可能只是局部变量:
self.parser = ArgumentParser(usage='To check what is the cloud name')
parser.add_argument('cloud', nargs='?', default='cloud1') # optional argument with default value
args = .parser.parse_args()
self.cloud_name = args.cloud
对于仅标识单个可选参数,argparse
可能有点矫枉过正。 sys.argv
就够了:
...
self.config_file_pass = os.path.expanduser('~/PycharmProjects/ui/config.cfg')
self.cloud_name = sys.argv[1] if len(sys.argv) > 1 else 'cloud1'
好的,假设我有这部分代码(它并不完美)。我想要这种情况 - 我正在检测云名称作为 __init__
中的参数,因此所有其他模块和脚本都将 运行 在该云上,或者如果我想 运行 特定 python 来自终端的文件 我可以检测到我希望它在其中 运行ned 的云 python my_script.py cloud1
执行此操作的最佳方法是什么?
当我使用参数从终端 运行 宁它时,以下脚本确实有效,但如果不是,它会给出此错误
usage: To check what is the cloud name config_parser.py: error: too few arguments'
这是一个代码
class CredentialsCP:
def __init__(self, cloud_name=None):
self.config = ConfigParser.ConfigParser()
self.cloud_name = cloud_name
self.config_file_pass = os.path.expanduser('~/PycharmProjects/ui/config.cfg')
self.parser = ArgumentParser(usage='To check what is the cloud name')
self.parser.add_argument('cloud')
self.args = self.parser.parse_args()
if self.args:
self.cloud_name = self.args.cloud
if self.cloud_name is None:
self.cloud_name = 'cloud1'
我有一个显示云的 url 的函数,它是如何调用的
ArgumentParser
提供了可选参数,但位置(非标志)参数的默认设置是 必需的 。在这里你可以使用:
self.parser = ArgumentParser(usage='To check what is the cloud name')
self.parser.add_argument('cloud', nargs='?', default='cloud1') # optional argument with default value
self.args = self.parser.parse_args()
self.cloud_name = self.args.cloud
可能的改进:
在这段代码中,parser
和args
是class的成员属性。如果它们不在 __init__
方法之外使用,它们可能只是局部变量:
self.parser = ArgumentParser(usage='To check what is the cloud name')
parser.add_argument('cloud', nargs='?', default='cloud1') # optional argument with default value
args = .parser.parse_args()
self.cloud_name = args.cloud
对于仅标识单个可选参数,argparse
可能有点矫枉过正。 sys.argv
就够了:
...
self.config_file_pass = os.path.expanduser('~/PycharmProjects/ui/config.cfg')
self.cloud_name = sys.argv[1] if len(sys.argv) > 1 else 'cloud1'