vscode python开发基本配置

1. 代码提示

pip安装pylint、flake8、pylint-django
setting.json配置如下:

"python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=120"
    ],
    "python.linting.pylintArgs": [
        "--load-plugins=pylint_django"
    ]

2. 远程开发和部署

1. Remote - SSH

用来远程连接到服务器,相当于直接把服务器上的文件用本地的vscode打开,直接进行编辑

2. SFTP

自己常用的模式,可以同步代码local-->remote或remote-->local。我一般喜欢在开发本地的项目,修改代码后先同步到remote,然后在remote上测试没问题的话再git提交。

{
    "name": "32",
    "host": "",
    "password": "",
    "protocol": "sftp",
    "port": 3222,
    "username": "bfd",
    "remotePath": "/opt/qa",
    "uploadOnSave": false,
    "syncMode": "update",
    "ignore": [
        "**/.vscode/**",
        "**/.git/**",
        "**/.idea/**"
    ]
}
SFT上传文件报错
  1. 问题详情
no such file or directory, open '/home/user/.ssh/config' load /home/user/.ssh/config failed
  1. 解决方法
    打开~/.vscode-server/extensions/liximomo.sftp-1.12.9/node_modules/ssh2-streams/lib/sftp.js,在WriteStream函数中添加一行options.autoDestroy = false;
function WriteStream(sftp, path, options) {
  if (options === undefined)
    options = {};
  else if (typeof options === 'string')
    options = { encoding: options };
  else if (options === null || typeof options !== 'object')
    throw new TypeError('"options" argument must be a string or an object');
  else
    options = Object.create(options);

  // For backwards compat do not emit close on destroy.
  options.emitClose = false;
  options.autoDestroy = false;   # 添加这一行

  WritableStream.call(this, options);
  1. 参考:
    https://github.com/liximomo/vscode-sftp/issues/919

3. 自动生成注释

可以通过File -> Preferences -> User Snippets创建一个python snippet,内容如下,分别是文件头和函数注释

{
    "HEADER":{
        "prefix": "fileheader",
        "body": [
            "#!/usr/bin/env python",
            "# -*- encoding: utf-8 -*-",
            "'''",
            "@File        :   $TM_FILENAME",
            "@CreateTime  :   $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
            "@Author      :   name ",
            "@Version     :   1.0",
            "@Contact     :   email",
            "@Description :   None",
            
            "'''",
            "",
            "# here put the import lib",
            "$0"
        ]
    },

    "COMMENT":{
        "prefix": "codecomment",
        "body": [
            "'''description",
            "$0",
            "Args:",
            "    param name (param type): describe the param",
            "$0",
            "Returns:",
            "    type: description",
            "'''"
        ],
    },

    // "COMMENT":{
    //     "prefix": "codecomment",
    //     "body": [
    //     "def $1($2):",
    //     "    \"\"\"",
    //     "    param $2:",
    //     "    return:",
    //     "    \"\"\"",
    //     "    $0"
    // ],
    // }
}

推荐阅读更多精彩内容