关于python:在VSCode中配置python3的调试环境

7次阅读

共计 3456 个字符,预计需要花费 9 分钟才能阅读完成。

应用虚拟环境

  • 设置虚拟环境
    应用 python 自带的 venv 创立一个虚拟环境

    python3 -m venv env

    执行下面的命令,会在我的项目根门路下生成一个 env 目录

  • 激活虚拟环境

    source ./env/bin/activate
  • 不应用虚拟环境

    deactivate
  • 装置第三方库

    pip3 install -r requirements.txt

    设置我的项目的环境变量

  • 配置环境变量文件 .env

    PYTHONUNBUFFERED=1
    ROBOT_DATA_PREFIX=/root
    PYTHONPATH=/root/IR_APP/app_server:/root/IR_APP/app_server/libs:/root/IR_APP/app_server/libs/common:/root/IR_APP/app_server/libs/controller_proto_pb2:/root/IR_APP/app_server/libs/controller_proto_pb2/protocal:/root/IR_APP/app_server/libs/controller_proto_pb2/message_type:$PYTHONPATH
  • 在 workspace 的配置文件中增加配置setting.json

    {
      "files.watcherExclude": {
          "**/android/**": true,
          "**/doc/**": true,
          "**/references/**": true,
          "**/test/**": true,
          "**/tp_log/**": true,
          "**/.git/objects/**": true,
          "**/.git/subtree-cache/**": true,
          "**/node_modules/*/**": true
      },
      "python.linting.pylintEnabled": false,
      "python.linting.flake8Enabled": true,
      "python.linting.enabled": true,
      "python.formatting.provider": "autopep8",
      // 指定环境配置文件
      "python.envFile": "${workspaceFolder}/.vscode/.env",
      // 设置我的项目的环境变量
      "terminal.integrated.env.linux": {
          "LC_ALL": "zh_CN.UTF-8",
          "ROBOT_DATA_PREFIX": "/root"
      },
      // linux: 在 VSCode 中配置 Terminal
      "terminal.integrated.shellArgs.linux": ["-c", "source /root/IR_APP/app_server/env/bin/activate"],
      // window: 在 VSCode 中配置 Terminal
      "terminal.integrated.shellArgs.windows": ["/k", "C:\\Virtualenv\\py3env\\Scripts\\activate"]
    }

    设置 Debug 的配置文件

  • 增加调试配置文件launch.json

    {
      // Use IntelliSense to learn about possible attributes.
      // Hover to view descriptions of existing attributes.
      // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
          {
              "name": "Current File",
              "type": "python",
              "request": "launch",
              "program": "${file}",
              "console": "integratedTerminal"
          },
          {
              // 调试时须要首先在终端中激活虚拟环境
              // source /root/IR_APP/app_server/env/bin/activate
              "name": "ir_app_boot",
              "type": "python",
              "request": "launch",
              "stopOnEntry": false,
              "cwd": "${workspaceFolder}",
              "program": "app/ir_app_boot.py",
              "args": ["-l", "tp", "--enabled-print"]
          },
          {
              "name": "pure_web_server",
              "type": "python",
              "request": "launch",
              "stopOnEntry": false,
              "cwd": "${workspaceFolder}",
              "program": "app/pure/pure_web_server.py",
              "args": ["-l", "tp", "--enabled-print"]
          },
          {
              "name": "status_server_web",
              "type": "python",
              "request": "launch",
              "stopOnEntry": false,
              "cwd": "${workspaceFolder}",
              "program": "app/nodes/status_server_web.py",
              "args": ["-l", "tp", "--enabled-print"]
          },
          {
              "name": "controller_req_proxy",
              "type": "python",
              "request": "launch",
              "stopOnEntry": false,
              "cwd": "${workspaceFolder}",
              "program": "app/nodes/controller_req_proxy.py",
              "args": ["-l", "tp", "--enabled-print"]
          },
          {
              "name": "active_alarm_service",
              "type": "python",
              "request": "launch",
              "stopOnEntry": false,
              "cwd": "${workspaceFolder}",
              "program": "app/nodes/active_alarm_service.py",
              "args": ["-l", "tp", "--enabled-print"]
          },
          {
              "name": "log_history_service",
              "type": "python",
              "request": "launch",
              "stopOnEntry": false,
              "cwd": "${workspaceFolder}",
              "program": "app/nodes/log_history_service.py",
              "args": ["-l", "tp", "--enabled-print"]
          },
          {
              "name": "files_manager_server",
              "type": "python",
              "request": "launch",
              "stopOnEntry": false,
              "cwd": "${workspaceFolder}",
              "program": "app/nodes/files_manager_server.py",
              "args": ["-l", "tp", "--enabled-print"]
          },
          {
              "name": "active_alarm_service_test",
              "type": "python",
              "request": "launch",
              "stopOnEntry": false,
              "cwd": "${workspaceFolder}",
              "program": "integration_test/active_alarm_service_test.py",
              "args": ["-l", "tp", "--enabled-print"]
          },
          {
              "name": "tp_hardware_web_service",
              "type": "python",
              "request": "launch",
              "stopOnEntry": false,
              "cwd": "${workspaceFolder}",
              "program": "app/nodes/tp_hardware_web_service.py",
              "args": ["-l", "tp", "--enabled-print"]
          }
      ]
    }
正文完
 0