调试打算从终端运行的python应用程序

调试打算从终端运行的python应用程序,python,bash,visual-studio-code,ide,Python,Bash,Visual Studio Code,Ide,我想解决由其他人编写的复杂python应用程序的bug。我想设置VisualStudio代码[VSC]及其调试器,以便逐步完成代码 python应用程序应该使用参数从linux终端运行,而不是像python那样运行。因此,我无法正确设置VSC,也无法用python运行应用程序的命令。例如,我使用两个文件重新创建了应用程序: 文件1 myprog: #!/usr/bin/env python import sys def format(): #print(' myprog > fo

我想解决由其他人编写的复杂python应用程序的bug。我想设置VisualStudio代码[VSC]及其调试器,以便逐步完成代码

python应用程序应该使用参数从linux终端运行,而不是像python那样运行。因此,我无法正确设置VSC,也无法用python运行应用程序的命令。例如,我使用两个文件重新创建了应用程序:

文件1 myprog:

#!/usr/bin/env python
import sys
def format():
    #print('  myprog > format: sys.argv=',sys.argv)
    import myprog   
    myprog.format_doc()

def main():
    if len(sys.argv) >= 2:
        command = sys.argv[1] 
        if command == '--version':
            print('V1.0')
        elif command == 'format':
            eval(command+'()')
main()
文件2 myprogr.py:

import os, sys
def format_doc():
    print('  myprog.py > format_doc: sys.argv=',sys.argv)
    print('  more complicated code I want to debug using several arguments')
当我从终端运行应用程序时,我看到参数被传递到sys.argv中的File2(我认为这是eval的结果):

我可以设置VSC来运行上面的bash命令并进入python代码吗

或者,我可以从python运行File2中的方法,以便在VSC中轻松调试它吗?如您所见,我无法使用python添加参数,因为python方法不接受任何输入参数:

>>> import myprog
>>> myprog.format_doc()
  myprog.py > format_doc: sys.argv= ['']
  more complicated code I want to debug using several arguments
>>> myprog.format_doc("hello.txt","--someoption","or_maybe_some_option")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: format_doc() takes 0 positional arguments but 1 was given
导入myprog >>>myprog.format_doc() myprog.py>格式文件:sys.argv=[''] 我想用几个参数调试更复杂的代码 >>>myprog.format\u doc(“hello.txt”,“--someoption”,“或者可能是某个选项”) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 TypeError:format_doc()接受0个位置参数,但给出了1个
您考虑过使用吗?我从未尝试过,但是:
sys.argv=[“hello.txt”…]
然后
myprog.format\u doc()
@quamrana是的,它很简单,而且很简单works@quamrana在我的实际应用程序中,我需要添加脚本的位置(在上面文件1的示例中):sys.argv=[“/home/myusername/anaconda3/bin/the_real_application”,“hello.txt”,“--html\u output=hello.html”]您考虑过使用吗?我从未尝试过,但是:
sys.argv=[“hello.txt”…]
then
myprog.format\u doc()
@quamrana是的,这很简单,而且works@quamrana在我的实际应用程序中,我需要添加脚本的位置(在上面的文件1示例中):sys.argv=[“/home/myusername/anaconda3/bin/the_real_应用程序”、“hello.txt”、“--html_output=hello.html”]
>>> import myprog
>>> myprog.format_doc()
  myprog.py > format_doc: sys.argv= ['']
  more complicated code I want to debug using several arguments
>>> myprog.format_doc("hello.txt","--someoption","or_maybe_some_option")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: format_doc() takes 0 positional arguments but 1 was given