最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【记录】用VSCode开发和调试Python

Python crifan 770浏览 0评论

之前用PyCharm去开发Python,体验很好,就是IDE太重了,资源消耗有点多。

之前已经试了试轻量级的VSCode的调试Python的效果:

【已解决】VSCode中调试Python代码

现在有机会去,继续用VSCode去开发Python代码,试试感觉如何:

好像还不错

继续去试试。

先去处理json加载

python json

用如下代码:

<code>#!/usr/bin/python
# -*- coding: utf-8 -*-

BookJsonTemplateFilename = "book_common.json"
CurrentGitbookName = "youdao_note_summary"
BookJsonCurrentFilename = "book_current.json"

import os
import json

templateJson = {}
currentJson = {}

currPath = os.getcwd()
print("currPath=%s" % currPath)
bookJsonTemplateFullPath = os.path.join(currPath, BookJsonTemplateFilename)
print("bookJsonTemplateFullPath=%s" % bookJsonTemplateFullPath)
# /Users/crifan/GitBook/Library/Import/book_common.json
with open(bookJsonTemplateFullPath) as templateJsonFp:
    templateJson = json.load(templateJsonFp)
    # print("type(templateJson)=%s" % type(templateJson)) #type(templateJson)=&lt;type 'dict'&gt;

bookJsonCurrentFullPath = os.path.join(currPath, CurrentGitbookName, BookJsonCurrentFilename)
print("bookJsonCurrentFullPath=%s" % bookJsonCurrentFullPath)
with open(bookJsonCurrentFullPath) as currentJsonFp:
    currentJson = json.load(currentJsonFp)

print("templateJson=%s" % templateJson)
print("currentJson=%s" % currentJson)
</code>

可以加载已有的两个json文件并打印内容了:

去把Python从2.7.10换成3.6:

出现提示是否安装pylint,点击Install:

<code>/usr/local/bin/python3 -m pip install -U pylint --user
➜  Import /usr/local/bin/python3 -m pip install -U pylint --user
Collecting pylint
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/f2/95/0ca03c818ba3cd14f2dd4e95df5b7fa232424b7fc6ea1748d27f293bc007/pylint-1.9.2-py2.py3-none-any.whl (690kB)
    100% |████████████████████████████████| 696kB 55kB/s
Collecting six (from pylint)
  Using cached https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting astroid&lt;2.0,&gt;=1.6 (from pylint)
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/0e/9b/18b08991c8c6aaa827faf394f4468b8fee41db1f73aa5157f9f5fb2e69c3/astroid-1.6.5-py2.py3-none-any.whl (293kB)
    100% |████████████████████████████████| 296kB 36kB/s
Collecting isort&gt;=4.2.5 (from pylint)
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/1f/2c/22eee714d7199ae0464beda6ad5fedec8fee6a2f7ffd1e8f1840928fe318/isort-4.3.4-py3-none-any.whl (45kB)
    100% |████████████████████████████████| 51kB 55kB/s
Collecting mccabe (from pylint)
  Cache entry deserialization failed, entry ignored
  Using cached https://files.pythonhosted.org/packages/87/89/479dc97e18549e21354893e4ee4ef36db1d237534982482c3681ee6e7b57/mccabe-0.6.1-py2.py3-none-any.whl
Collecting lazy-object-proxy (from astroid&lt;2.0,&gt;=1.6-&gt;pylint)
  Cache entry deserialization failed, entry ignored
  Using cached https://files.pythonhosted.org/packages/55/08/23c0753599bdec1aec273e322f277c4e875150325f565017f6280549f554/lazy-object-proxy-1.3.1.tar.gz
Collecting wrapt (from astroid&lt;2.0,&gt;=1.6-&gt;pylint)
  Cache entry deserialization failed, entry ignored
  Using cached https://files.pythonhosted.org/packages/a0/47/66897906448185fcb77fc3c2b1bc20ed0ecca81a0f2f88eda3fc5a34fc3d/wrapt-1.10.11.tar.gz
Building wheels for collected packages: lazy-object-proxy, wrapt
  Running setup.py bdist_wheel for lazy-object-proxy ... done
  Stored in directory: /Users/crifan/Library/Caches/pip/wheels/a0/63/e2/6d93295282cb35b53b14b50b602c76dfb04471e21b31d8ad7b
  Running setup.py bdist_wheel for wrapt ... done
  Stored in directory: /Users/crifan/Library/Caches/pip/wheels/48/5d/04/22361a593e70d23b1f7746d932802efe1f0e523376a74f321e
Successfully built lazy-object-proxy wrapt
Installing collected packages: six, lazy-object-proxy, wrapt, astroid, isort, mccabe, pylint
Successfully installed astroid-1.6.5 isort-4.3.4 lazy-object-proxy-1.3.1 mccabe-0.6.1 pylint-1.9.2 six-1.11.0 wrapt-1.10.11
➜  Import
</code>

这样就能用

import pprint

了。

然后再去想办法看看,如何合并json,或者说dict:

【已解决】Python中实现dict的递归的合并更新

然后再去:

【已解决】python中把dict的json输出到文件且带缩进和不要unicode的\uxxxx

如此,才可以生成所需要的book.json

但是字段的顺序,不是希望的。所以再去:

【部分解决】Python输出dict(json)到文件中时确保或指定字段的顺序

至此,用VSCode去调试Python,效果还是不错的。

此处用到的一些功能:

【调试的快捷键】

  • 开始调试:F5

  • 停止:Shift+F5

  • 重新开始:Command+Shift+F5

  • 单步(跳过):F10

【点击行首添加断点】

【console终端】

清除之前的log:

右键-》清除

==Command+K

VSCode调试Python总体感觉:

速度快

方便好用

不过功能上,还是没有PyCharm强大。

结论:

对于普通小项目,文件少的话,用VSCode,已经算很不错了。

很大的项目,再考虑用PyCharm。

转载请注明:在路上 » 【记录】用VSCode开发和调试Python

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
54 queries in 0.122 seconds, using 18.27MB memory