使用虚拟环境

  • 设置虚拟环境
    使用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" ]
          }
      ]
    }

alits
7 声望0 粉丝