如何在多部分命令行语句中以调试模式运行 Pycharm [英] how to run Pycharm in debug mode on a multi-part command-line statement

查看:41
本文介绍了如何在多部分命令行语句中以调试模式运行 Pycharm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能令人困惑,所以如果您想到更好的,请更改它或告诉我,我会更改它.

假设你有这个命令:

cat f1.txt f2.txt | python script.py cmd1 | ... | python script.py cmd2 | ...

如何启动 Pycharm 调试器并单步执行 script.py 的代码?

How can I launch the Pycharm debugger and single-step through the code of script.py?

这是在 Windows 上.

This is on windows.

推荐答案

PyCharm 具有通过运行/调试配置运行任务的功能.我将在这里尝试与他们一起模拟您的要求.根据您的需要进行调整.

PyCharm has the feature to run tasks via Run/Debug configurations. I will try to emulate your requirement with them here. Adapt it according to your needs.

  1. 选择编辑配置:

  1. 点击添加按钮 + 并选择 Bash

  1. 输入您希望使用 /bin/bash 运行的 bash 脚本.如果它是单个命令,那么我的解决方法是将第一个参数放在脚本名称的位置,然后将第二个参数放在 Program Argument 空间.
  1. Input the bash script that you wish to run with /bin/bash. If it is a single command then, my workaround is to put the first argument in the script name's place and the second argument onwards at the Program Argument space.

  1. 切换到左侧选项卡中的 Python > your_script 并在 Before launch: Activate tool window 中添加现在创建的任务.点击+ > Run Another Configuration,选择上一步添加的bash配置.它应该类似于以下内容:
  1. Switch to Python > your_script in the left tab and add the now created task in the Before launch: Activate tool window. Click + > Run Another Configuration, select the bash config added in the previous step. It should look something like the one below:

  1. 单击确定并关闭运行/调试配置窗口.
  2. 设置断点,选择要调试的配置,然后单击调试"按钮.

  1. 重复步骤 2 到 4 多次以添加多个命令.

对于复杂的多命令操作,我还没有真正尝试过这样做.我通常将其分解并单独调试脚本.您可以考虑将输出保存到文件中并使用它们.

I haven't really tried to do this for a complex multi command operation. I usually break it down and debug the script separately. You can consider saving the output to files and using them.

警告:

从您的示例命令看来,您正在将一个命令的输出通过管道传输到另一个命令,我还没有想出一种将一个配置的输出转发到另一个配置的方法.

From your example command it looks like you are piping the output of one command to another, I haven NOT yet figured out a way to forward the output of one configuration into another.

可能的解决方法:

  1. 如果它们都是 Python 脚本,那么您可以尝试使用 Logs 选项卡中的选项将每个输出保存到文件.
  1. If they are all Python scripts, then you could try to save each output to file using the option from Logs tab.

  1. 遗憾的是Bash Configuration 没有保存输出功能.解决方法可能是编写一个 bash 文件,将输出保存到一个文件中,然后在 Bash 配置 中运行该 bash 脚本.
  1. Sadly Bash Configuration doesn't have a save output feature. The work around could be to write a bash file that saves the output to a file and run that bash script in the Bash Configuration.

# contents of cat_files.sh
cat file1.txt file2.txt > outfile.txt

现在在 python 脚本配置中,您可以将 outfile.txt 作为参数传入并读取它以获取数据.

Now in the python script config you could pass in outfile.txt as a parameter and read it to get the data.

这篇关于如何在多部分命令行语句中以调试模式运行 Pycharm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆