2017-08-25 34 views
0

有問題的程序是Pydial。我用這個命令在pycharm中運行它。調試僅在命令行中運行的python程序

蟒蛇pydial.py聊天的config /嘖嘖HDC-CamInfo.cfg

我只能在我的終端上運行。我正在嘗試調試程序,但只是不運行程序。

如何在運行時調試程序?我想看看程序運行時正在執行的類和方法的順序,而不是當前的情況,我可以看到正在執行的類和方法的順序,但程序不運行(我無法給出任何輸入該程序)。

+0

? – Sayse

+0

但我沒有任何配置參數 – Sid

+1

閱讀[pdb](https://docs.python.org/2/library/pdb.html)和[python-debugging-tips](https://stackoverflow.com/問題/ 1623039/Python的調試-TIPS) – stovfl

回答

0

要在腳本開始處開始調試,請添加-m pdb。進入調試器後,輸入n執行下一行,s進入功能,或exit停止調試。輸入?以查看所有的pdb選項。

python -m pdb pydial.py chat config/Tut-hdc-CamInfo.cfg 
> /home/jdb-work/repos/pydial/pydial.py(25)<module>() 
-> import os 
(Pdb) list 
20  # See the License for the specific language governing permissions and 
21  # limitations under the License. 
22  # 
23  ############################################################################### 
24  
25 -> import os 
26  from scriptine import run, path, log, command 
27  import re 
28  import numpy as np 
29  
30  # Uncomment for mac os users 
(Pdb) ? 

Documented commands (type help <topic>): 
======================================== 
EOF bt   cont  enable jump pp  run  unt 
a  c   continue exit l  q  s  until 
alias cl   d   h  list quit  step  up 
args clear  debug  help n  r  tbreak w  
b  commands disable ignore next restart u  whatis 
break condition down  j  p  return unalias where 

Miscellaneous help topics: 
========================== 
exec pdb 

Undocumented commands: 
====================== 
retval rv 

(Pdb) exit 

如果您知道要調試的代碼行,你可以插入import pdb; pdb.set_trace()直接進入腳本,然後正常運行的命令。這將在該確切點停止執行。

python pydial.py chat config/Tut-hdc-CamInfo.cfg 

另一個可以安裝的調試器是IPython調試器ipdb。要使用它,您可以添加-m ipdb或插入import ipdb; ipdb.set_trace()並再次正常運行該命令。

下面是代碼重現我的例子:如果你在pycharm運行它,然後只是做一個運行配置

git clone [email protected]:dialoguesystems/pydial.git 
cd pydial 
# Switch the commit I used when creating this example 
git checkout c215003f25b959ee350e192079d88e138e6e8dbf 
# Create a conda environment 
conda create -n pydial python=2.7 ipython ipdb 
source activate pydial 
pip install -r requirements.txt 
# All the requirements weren't in requirements.txt 
pip install scriptine matplotlib 
python -m pdb pydial.py chat config/Tut-hdc-CamInfo.cfg