要在腳本開始處開始調試,請添加-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
? – Sayse
但我沒有任何配置參數 – Sid
閱讀[pdb](https://docs.python.org/2/library/pdb.html)和[python-debugging-tips](https://stackoverflow.com/問題/ 1623039/Python的調試-TIPS) – stovfl