Python with VS code

Suci Lin
Suci Lin
May 1 · 5 min read

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


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
Change Python interpreter

(iii) Create a terminal for the active interpreter of the current virtual environment

⌘Cmd + shift + p => Search and choose "Python:Create Terminal
Create a terminal for the current Python interpreter

b. 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)

Linter

Formatters

Snippets

Theme

Other

4. Know More Tips

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:

  1. https://code.visualstudio.com/docs (VS code official site)
  2. https://code.visualstudio.com/docs#python-articles
  3. https://devblogs.microsoft.com/python/ (latest update about python in Microsoft)
  4. Getting Started with Python in VS Code (Great tutorial)
  5. https://twitter.com/pythonvscode
  6. Configuring Python environments (from the official site)
  7. keyboard-shortcuts (from the official site)
  8. Get Productive with Python and VS Code (EuroPython 2018, recommend to watch for beginners)
  9. Boost Your Coding Fu With Visual Studio Code and Vim (If you like vim as well)

Suci Lin

Written by

Suci Lin

Data Engineer, focus on stream processing and IoT. Passionate about data storytelling with Data Visualization and engineering culture within an organization.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade