使用Pycharm调试控制台脚本

5

我有一个复杂的Python程序需要调试,其中setup.py文件有

 entry_points=dict(
      console_scripts=[
          'myprog = myprog.command:myprog_main',
      ]
  )

在这里,command.py有接受命令的逻辑,因此我可以运行类似以下的命令:

myprog process --config config.yaml 

在PyCharm中设置断点并不会使程序停止,因为执行python command.py process --config config.yaml不会做任何事情。我觉得这是一些基础知识,但我无法找到调试此问题的方法(使用PyCharm)。
2个回答

2
让我们以jupyter notebook为例:
jupyter中,它使用from jupyter_core.command import main,所以我需要做的就是在jupyter_core.command:main中放置一个断点。
然后,我需要在Pycharm中添加配置。 Script path应该是/path/to/jupyterParameters应该是notebook
接下来,我需要点击Debug
完成了,我已经到达了jupyter_core.command:main中的断点。

1

如果这个答案对你不起作用,请尝试以下方法:

  1. add a run configuration in PyCharm

  2. configure it with Module name instead of Script path.

    enter image description here

  3. Set the Module name to myprog.command

  4. Add the following section to myprog/command.py:

    if __name__ == "__main__":
        myprog_main()
    

    Sadly, setting the Module name to myprog.command:myprog_main doesn't work.

  5. Set the Working directory to the root of your repo (i.e. the parent of the module myprog such that the imports work).

注意:我使用了提问者问题中的确切名称。请根据您的问题相应地调整函数、模块和包的名称。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接