Python with VS code
After working with several different source code editors, started with Vim, Atom, and Pycharm/IntelliJ, and the recent testing is Visual Studio Code. My favorite is still Vim. However, I still spent much time to switch between Pycharm/IntelliJ and VS Code for some testing 😳.
I really like VS Code’s light-weight, highly customizable with many extensions and it is open source. I keep VS Code as my favorite and default editor in GUI-based OS (eg. MacOS). But for larger code bases, I sometimes consider switching to Pycharm.
Recently, I watched and learned some things new from a great sharing of EuroPython 2018 for Python and VS Code. It is inspiring me to write down how I use VS Code with Python (general development workflow, configuration with some extensions) and share it. You may find somethings helpful.
PRE-REQUISITES
- Basic knowledge of Python
- Install Python 3 in your OS (I use pyenv to switch different versions of Python)
- (Optional) Go through VS Code Getting Started Doc to know the basic usages
- Download and install the VS Code
- Install the Python extension for VS Code
- (Optional) Install more extensions for newly installation or backup by Settings Sync or well-defined recommended extensions
Getting Started
1. General Development Workflow
My development flow for python projects with a well-configured VS Code is as the followings:
a. Preparation Stage
- (prerequisite) Create a new virtual environment for Python interpreter (Optional, if not exist for this project)
(i) Start VS Code (in a project folder as the below). The folder will be considered as “workspace” in VS Code.
mkdir py_project
cd py_project
code Or open my working Workspace/Project/Folder/File (File > Open Folder)
* Open a Folder / Project — ⌘Cmd + o
* Create a new file — ⌘Cmd + n
* Open the terminal pane — Ctrl + `(ii) Select a Python interpreter (if need to change)
- open Command Palette (⇧⌘P) => Python: Select Interpreter
(iii) Create a terminal for the active interpreter of the current virtual environment
⌘Cmd + shift + p => Search and choose "Python:Create Terminalb. Developing Stage
(i) Coding (after defining purposes and necessary system design)
Editing code
- Working with Autocomplete and IntelliSense
- Make code more readable and conventional with Formatting (autopep8) and Linting (Pylint)
- Refactoring with Extract Variable, Extract Method and Sort Imports
- See more details on editing, formatting, and refactoring in Editing code (VS Code official doc).
Select code and run
- After finishing a part of coding, you may want to try it to see if it works as you expected.
- Select one or more lines, then press Shift+Enter or right-click (Python: Run Selection/Line in Python Terminal). The part of the code will be executed.
Save the code
- Save (⌘S)
(ii) Debugging
General debugging flow1.Add debugging breaking points
2.Switch the debugging mode by "Starting Debugging" (F5)
3.Check code, variables and behaviors
4.Debug Console
5.With debugging actions to check code, variables and behaviors:
* "Step Into" (F11) "Step Out" (Shift F11)
* "Step Over" (F11)
* "Run to Cursor", one time breaking point
* Continue debugging (F5):jump to the next breaking points
https://code.visualstudio.com/docs/python/python-tutorial#_configure-and-run-the-debugger
(iii) Testing
- Discover Unit Tests
- Enable and configure test framework
- Select test folder to run the test
2. Configuration
Some configurations I may change
Edit Settings (Ctrl/⌘cmd +,)
(i) Configure the Python extension (Setup the Python interpreter path)
(ii) Use environment variables
(iii) Code Linting
- Customize pylint (.pylintrc) (ref)
(iv) Code Formatting
(v) Unit tests
3. Know More Extension
Some extensions I used to make my code editing easier. (changed time to time)
Get Suci Lin’s stories in your inbox
Join Medium for free to get updates from this writer.
Linter
Formatters
Snippets
Theme
Other
- amVim
- Bracket Pair Colorizer
- GitLens — Git supercharged
- Markdown All in One
- Project Manager
- Settings Sync
- TODO Highlight
4. Know More Tips
- Familiar some Key Shortcut (VS Code Tips and Tricks)
- Customized key binding
- Creating Your Own Snippets
- Use built–in terminal emulator
- Working with Docker
- Git/ VCS integration (just convenient, may not fully functional if compared with source tree or other GUI)
- Working with Jupyter Notebooks
Conclusion
In this article, I showed how I use VS code generally (basic development flow). I don’t write too much about my configuration, refactoring, and some other tips and my habits. I may update it time to time. However, the official website of VS code actually contains a lot of useful articles and tutorial. There are more detailed functions and tips to save you time in your everyday development lifecycle.
Using more and taking more advantage of all of Visual Studio Code’s capabilities will maximize my productivity. I believe my working flow with VS Code will be changed and improved time to time.
本文分享我使用VS code 進行python 程式的開發流程 (但我實際上最喜歡使用Vim)
❶開發環境的預先準備
- 相關程式的安裝 (Python, VS Code和 extension)
❷開發流程
(i) Coding
- 開 VS code ->開working Workspace/Project/Folder/File
- 新的專案要建立新的 virtual environment for Python interpreter
- 確認是否需要切換虛擬環境或是 interpreter
- edit
- (select code) and run
- save
(ii) Debugging
- 加breaking point,
- ‘F5’ run debugging
- Stepping並同時 Inspecting 變數 和利用Debug
- 也可執行Run to Cursor
(iii) Testing
- Discover Unit Tests
- Enable and configure test framework
- Select test folder to run the test
官方網站其實有很多參考文件,可以協助學習如何建置更進階的VS Code環境和許多進階用法。
Reference:
- https://code.visualstudio.com/docs (VS code official site)
- https://code.visualstudio.com/docs#python-articles
- https://devblogs.microsoft.com/python/ (latest update about python in Microsoft)
- Getting Started with Python in VS Code (Great tutorial)
- https://twitter.com/pythonvscode
- Configuring Python environments (from the official site)
- keyboard-shortcuts (from the official site)
- Get Productive with Python and VS Code (EuroPython 2018, recommend to watch for beginners)
- Boost Your Coding Fu With Visual Studio Code and Vim (If you like vim as well)










