简体   繁体   English

如何使用python3 -i加载脚本,但没有名称==主运行?

[英]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 * . 只需运行python3from 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.

相关问题 如何从 Python3 脚本运行 Gradle - How to run Gradle from a Python3 script 如何用python3运行ansible清点脚本 - How to run ansible inventory script with python3 如何在带有 if __name__='__main__' 块的 Python3 中使用相对导入? - How can I use relative importing in Python3 with an if __name__='__main__' block? 如何为正确的名称运行 python 命令(py、python、python3) - How can I run a python command for the correct name (py, python, python3) 在 Python 中,如何同时从主脚本运行另一个 Python 脚本并在停止主脚本时将其关闭? - In Python, how can I run another Python script from the main script at the same time and close it when I stop the main script? 如何让 python selenium webdriver 脚本 24/7 全天候运行? - How can I have a python selenium webdriver script run 24/7? 如何在终端Visual Studio Code中运行Python3函数脚本 - How to run Python3 function script in terminal Visual Studio Code 如何通过ssh远程运行python3脚本 - How to run a python3 script remotely trough ssh 如何从主 python 文件正确运行单独的 python 脚本? - How do I properly run a separate python script from main python file? 加载环境以运行Python脚本的成本是多少? - How expensive it is to load the environment to run a Python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM