VSCode中配置Python虚拟环境的Debugger


3 Ways to set VS code Debugger for Python with virtul envrioment

首先是我的文件目录:

my_project
    - .vscode
        - launch.json
        - settings.json
    - package
        - __init__.py
        - utils.py
        - play.py
    - play.py

然后需要指定pipenv的环境为默认的python环境,在settings.json里设置:

{
  "python.pythonPath": "/Users/smap10/.local/share/virtualenvs/my_project-s2afkbP_/bin/python",
}

第一种debug方式,在play.py

from pathlib import Path

from tqdm import tqdm

from package.utils import save_json

print('haha')

然后在launch.json里配置和debug相关的内容。主要是设置cwd为my_project:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "cwd": "/Users/smap10/Project/my_project/"
    }
  ]
}

“program”: “${file}”, 这个表示我们直接对当前窗口的文件进行debug。

点击debug按钮,没有问题

▶ /Users/smap10/.local/share/virtualenvs/strike-esg-head-office-s2afkbP_/bin/python /Users/smap10/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/new_ptvsd/no_wheels/ptvsd/launcher /Users/smap10/Project/strike-esg-head-office/play.py 
haha

第二种,在package里的utils.py

from pathlib import Path

from tqdm import tqdm

from package.utils import save_json

print('haha')

然后在launch.json里配置和debug相关的内容。主要是设置cwd为my_project:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      // "program": "${file}",
      "console": "integratedTerminal",
      "cwd": "/Users/smap10/Project/strike-esg-head-office/",
      "module": "esg_research.preprocess.utils",

    }
  ]
}

记得把program注释掉,直接通过module添加要debug的文件

点击debug按钮,没有问题

▶ env PTVSD_LAUNCHER_PORT=64165 /Users/smap10/.local/share/virtualenvs/strike-esg-head-office-s2afkbP_/bin/python /Users/smap10/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/new_ptvsd/no_wheels/ptvsd/launcher -m esg_research.preprocess.utils 
haha

第三种,在package里的play.py需要传入参数

Play.py里需要传入参数:

def main(args: Namespace) -> None:

    # Get validate_files from txt floder
    PREFIX = args.txt_path
    SAVE_PATH = args.save

    # Validate process
    print('Find valid PDF files...')
    doc_paths = glob.glob(os.path.join(PREFIX, '*'))
    valid_docs, valid_docs_set = get_valid_docs(doc_paths)
    print(f'Total docs (PDF): {len(doc_paths)}')
    print(f'Valid docs: {len(valid_docs)}')
    print(f'Invalid docs: {len(doc_paths) - len(valid_docs)}')
    print('Done!')

    # Save result for later use
    save_json(valid_docs, os.path.join(SAVE_PATH, 'valid_docs.json'))
    save_json(list(valid_docs_set), os.path.join(SAVE_PATH, 'valid_docs_set.json'))
    print(f'Save valid docs to {SAVE_PATH}...')
    print('Done!')


if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument(
        '--txt_path',
        type=str,
        required=True,  # e.g. '/home/ec2-user/SageMaker/strike-esg-head-office/data/txt'
        help='The path of txt directory, which contains the parsed txt files from PDF'
    )
    parser.add_argument(
        '--save',
        type=str,
        required=False,
        default='data/output',
        help='The path of directory to save the valid_docs, and valid_docs_set'
    )
    args = parser.parse_args()
    main(args)

更改launch.json,添加args:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      // "program": "${file}",
      "console": "integratedTerminal",
      "cwd": "/Users/smap10/Project/strike-esg-head-office/",
      "module": "esg_research.preprocess.validate_docs",
      "args": [
        "--txt_path", "/Users/smap10/Project/strike-esg-head-office/data/txt",
        "--save", "/Users/smap10/Project/strike-esg-head-office/data/output"
      ]
    }
  ]
}

可以看到终端里的显示是没有问题的:

▶ env PTVSD_LAUNCHER_PORT=64295 /Users/smap10/.local/share/virtualenvs/strike-esg-head-office-s2afkbP_/bin/python /Users/smap10/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/new_ptvsd/no_wheels/ptvsd/launcher -m esg_research.preprocess.validate_docs --txt_path /Users/smap10/Project/strike-esg-head-office/data/txt --save /Users/smap10/Project/strike-esg-head-office/data/output 

欢迎订阅我的博客:RSS feed
知乎: 赤乐君
Blog: BrambleXu
GitHub: BrambleXu
Medium: BrambleXu


文章作者: BrambleXu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 BrambleXu !
评论
  目录