[英]How to load a script with python3 -i, but not have name == main run?
I have a script that I want to use with the python shell (interactivity mode, live interpreter, python3 [return]
) and the script I want to have added to the live interpreter ( python3 -i script.py
) has a if __name__ == '__main__':
in it. 我有一个要与python shell一起使用的脚本(交互模式,实时解释器, python3 [return]
),我想添加到实时解释器中的脚本( python3 -i script.py
)具有if __name__ == '__main__':
在其中。 When I load the script the if __name__
runs. 当我加载脚本时, if __name__
运行。 I have argsprase in the if __name__
which spits out an error. 我在if __name__
有argsprase,它吐出一个错误。 So, I need to be able to add the script to the live interpreter, but not have certain code in the script run, the code in if __name__
. 因此,我需要能够将脚本添加到实时解释器中,但是脚本中没有某些代码可以运行, if __name__
, if __name__
。
script.py script.py
#/usr/bin/python3
class myExcellentClass:
def __init__(var1, var2):
self.var1 = var1
self.var2 = var2
def WhatisVar1(self):
return self.var1
def WhatisVar2(self):
return self.var2
if __name__ == '__main__':
import argparse
# setup args parse
# do some stuff here
I'm thinking there must be a variable that I can add to if __name__
that will test for whether the script is being run with -i
or not. 我在想if __name__
必须添加一个变量,它将测试脚本是否正在使用-i
运行。 For example: 例如:
if __name__ == '__main__' && is_interactive == false:
import argparse
# setup args parse
# do some stuff here
If there is a way to call the live interpreter from in side a python3 script, I would just add -i
to the script and have this launch the class added to the live interpreter. 如果有一种方法可以从python3脚本中调用实时解释器,则只需在脚本中添加-i
,然后将启动类添加到实时解释器中即可。
I could split out the class into another file. 我可以将班级分成另一个文件。 I would like not to do this if possible. 如果可能的话,我不想这样做。
Example: 例:
scriptA.py scriptA.py
#/usr/bin/python3
class myExcellentClass:
def __init__(var1, var2):
self.var1 = var1
self.var2 = var2
def WhatisVar1(self):
return self.var1
def WhatisVar2(self):
return self.var2
scriptB.py scriptB.py
#/usr/bin/python3
from scriptA import *
if __name__ == '__main__' && is_interactive == false:
import argparse
# setup args parse
# do some stuff here
I usually install the script system wide as a byte-code file for efficiency purposes. 为了提高效率,我通常将脚本系统安装为字节码文件。 (Yes I know that it's not recommend, nor will the pyc work with different versions of python.) As I only need to use the -i
for testing and trouble shooting, I would prepare a solution that would allow me to keep everything in one python file. (是的,我知道这是不推荐的,而且pyc也不能与不同版本的python一起使用。)由于我只需要使用-i
进行测试和排除故障,因此我将准备一个解决方案,使我可以将所有内容保存在一个python文件。
Just run python3
and the type from script import *
. 只需运行python3
并from script import *
输入类型即可。
a more complete answer here: What does if __name__ == "__main__": do? 这里有一个更完整的答案: __name__ ==“ __main__”怎么办?
The variable you are looking for actually exists. 您要查找的变量实际上存在。
from sys import flags
print(flags.interactive)
This prints 1
in interactive mode and zero otherwise. 在交互模式下将打印1
,否则将打印0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.